How can I get a list of all variables in a binding?

Does Binding know the variables it stores? Or should I always use this in conjunction with local_variables ?

+4
source share
2 answers

You will get a list of local variables in the binding by doing eval("local_variables", the_binding) .

I'm not sure what you meant by "Or should I always use this in conjunction with local_variables ?", But there is no way to get a list without calling local_variables some way.

+4
source

This is an old question, so the answer is correct for the Ruby version <= 2

But since 2.1 new methods have appeared to get / set and list local variables:

 local_variable_get / local_variable_set / local_variable_defined? 
0
source

Source: https://habr.com/ru/post/1298992/


All Articles