Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String to argument name

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

name = "foo"
func(name.to_sym: "whatever")

Is there a way?

Thanks.

>Solution :

func(name.to_sym => "whatever")

Seems to work in Ruby 3.1

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading