Is there a ‘variable_get’ method? If not, how can I create my own?

In Ruby 2.1 and later, you can use Binding#local_variable_get.

In prior versions of Ruby, you have to use eval. If you want to do some sanity-checking before evaluating a supposed variable name, you can check whether the named variable is in local_variables.

Leave a Comment