JackDanger/ruby — pull request #1 by JackDanger (2013-08-11 06:15:03+00:00)
Title: Allowing binding to list its local variables

Before this patch:

def get_all_local_variables(bind)
lvars = bind.send(:local_variables)
# `lvars` should equal [:x, :y], but equals [:bind, :lvars]
lvars.map {|name| bind.local_variable_get name }
end
x = 1
y = 2
get_all_local_variables(binding) # NameError: local variable `bind' not defined for #

After this patch:

def get_all_local_variables(bind)
lvars = bind.local_variables
# `lvars` equals [:x, :y]
lvars.map {|name| bind.local_variable_get name }
end
x = 1
y = 2
set_everything_but_x_to_5(binding) # => [1, 1]

bind.send(:local_variables) was a global method that used the current
stack frame regardless of binding. Now that we have
`Binding#local_variable_get` and `Binding#local_variable_set` the API
feels very strange without `Binding#local_variables` working similarly.