How to call a method with named parameters using a hash

-1

I would like to know if you can pass parameters to a named parameter method via hash. Can you review the remaining parameters for another function as in the example below?

params = {key: "value",key2: "value2"}
def func(key1: nil, key2: nil, **args)
    other_func **args
    ...
end
func params

Problem with rails:

#No controle
@session = SessionUser.new(params[:session_user].merge(session: session))

# classe SessionUser
def initialize(session: nil, email: nil, password: nil, **args)
    @session = session
    @email = email
    @password = password
end
    
asked by anonymous 11.02.2015 / 15:30

1 answer

0

I found the error, in case I passed a hash that had keys not symbols, the same problem was solved in this question:

link

In my case, using the rails I can use the '.symbolize_keys' for those who are just using Ruby it is also possible to solve this way:

my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
    
11.02.2015 / 18:18