I have a hashmap as below
func_hash = {
"update" => update(),
"delete" => delete(),
"get" => get()
}
The behaviour that I am observing is , while ruby trying to read this line, its also calling the function update().
How can I make it work like, call the function only when I call it with specific key like
func_hash["delete"]
Any help, much appreciated.
Thank you.
CodePudding user response:
you could do something like
func_hash = {
"update" => lambda { update() },
"delete" => lambda { delete() },
"get" => lambda { get() }
}
func_hash["delete"].call
CodePudding user response:
Use a lambda like Ursus suggested to just store the name of the function in the hash and use public_send
:
func_hash = {
"update" => [:update],
"delete" => [:delete],
"get" => [:get],
"complex" => [:method_name, 'argument', 'another_argument']
}
public_send(*func_hash['complex'])
#=> would call `method_name('argument', 'another_argument')`