Reputation: 65
Suppose I have a ruby function (func) with named arguments (foo and bar) which I can call by providing either or both arguments like this:
func(foo: "whatever")
func(bar: "whatever")
func(foo: "whatever", bar: "whatever")
What I need is a way to call this function by passing strings for the arguments' names:
name = "foo"
func(name: "whatever")
I read about to_sym but don't know how to use it. At least this does not work:
name = "foo"
func(name.to_sym: "whatever")
Is there a way?
Thanks.
Upvotes: 0
Views: 37
Reputation: 19
You can use the following:
func("#{name}": "whatever")
The name will be interpolated and converted to symbol
Upvotes: 1