mercredi 6 mai 2015

Check if local variable is defined given it's name as string in ruby

Can I check if a local variable is defined given it's name as string?

I know there is the function defined?, but you have to give the variable itself.

Example:

a = 'cat'
print defined?(a) # => "cat"
print defined?(b) # => nil

What I need is:

a = 'cat'
print string_defined?("a") # => "cat"
print string_defined?("b") # => nil

Or something like that. I can't find it in the docs...

I tried to use respond_to?, but doesn't seems to work...





Aucun commentaire:

Enregistrer un commentaire