Feature #8773

Binding#local_variables should work like #local_variable_set and #local_variable_get

Added by Jack Danger about 2 years ago. Updated over 1 year ago.

[ruby-core:56543]
Status:Closed
Priority:Normal
Assignee:-

Description

With the addition of Binding#local_variable_get and Binding#local_variable_set the following seemed reasonable:

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 #<Binding:0x0>

This is because `local_variables' is global and uses the current stack frame. That was not obvious to me. I could have just used binding.eval("local_variables") but that looked very strange when used alongside binding.local_variable_set and binding.local_variable_get.

Attached is a patch that gives Binding an instance method that properly lists the local variables defined in the binding. It now works like this:

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]

Here's a GitHub link if you want to see it with colors: https://github.com/JackDanger/ruby/pull/1/files

0001-Allowing-binding-to-list-its-local-variables.patch Magnifier - Create Binding#local_variables (3.66 KB) Jack Danger, 08/11/2013 03:21 PM

0001-Allowing-binding-to-list-its-local-variables.patch Magnifier - Allowing binding to list its local variables [2nd upload] (3.66 KB) Jack Danger, 08/11/2013 03:52 PM

Associated revisions

Revision 44392
Added by Nobuyoshi Nakada over 1 year ago

proc.c: Binding#local_variables

  • proc.c (bind_local_variables): allowing binding to list its local variables. patch by Jack Danger Canty at . [Feature #8773]

Revision 44392
Added by Nobuyoshi Nakada over 1 year ago

proc.c: Binding#local_variables

  • proc.c (bind_local_variables): allowing binding to list its local variables. patch by Jack Danger Canty at . [Feature #8773]

Revision 46639
Added by Usaku NAKAMURA about 1 year ago

  • NEWS: [DOC] mention about Binding#local_variables, introduced at r44392 (see [Feature #8773]).

Revision 46639
Added by Usaku NAKAMURA about 1 year ago

  • NEWS: [DOC] mention about Binding#local_variables, introduced at r44392 (see [Feature #8773]).

Revision 46644
Added by Nobuyoshi Nakada about 1 year ago

test_proc.rb: test_local_variables

  • test/ruby/test_proc.rb (test_local_variables): missing test for Binding#local_variables. [Feature #8773]

Revision 46644
Added by Nobuyoshi Nakada about 1 year ago

test_proc.rb: test_local_variables

  • test/ruby/test_proc.rb (test_local_variables): missing test for Binding#local_variables. [Feature #8773]

History

#1 Updated by Jack Danger about 2 years ago

I left a typo in the description field of this issue. The very last line of code in the example should read:

get_all_local_variables(binding) #=> [1, 2]

#2 Updated by Aaron Patterson about 2 years ago

@ko1 do you have any thoughts about this?

#3 Updated by Nobuyoshi Nakada over 1 year ago

  • Tracker changed from Bug to Feature

Sounds reasonable.

#4 Updated by Nobuyoshi Nakada over 1 year ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r44392.
Jack, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


proc.c: Binding#local_variables

  • proc.c (bind_local_variables): allowing binding to list its local variables. patch by Jack Danger Canty at . [Feature #8773]

Also available in: Atom PDF