In Ruby, you can easily access local variables programmatically by using local_variables
and eval
. I would really like to have meta-programming access to these variables using a single method call such as
# define a variable in this scope, such as
x = 5
Foo.explore_locals # inside the Foo#explore_locals method, access x
where Foo
is some external module. The idea is to display and export local variables in a nice way.
What should be inside the explore_locals
method? Is there any way to make this possible? If absolutely necessary, I guess it could be
Foo.explore_locals binding
but this is much less elegant for the application I have in mind.