Feature #12042
closedA better interface that returns a list of local variables available where the exception is raised
Description
We've changed the behavior of NameError#local_variables as discussed on #11777, but I'm not actually satisfied by the change. This change has made it impossible to get a list of local variables available within a NameError scope. It would be nice, if we could add a new method that does what NameError#local_variables does right now, and change the local_variables method back to the previous behavior.
I actually don't have a good name in mind, but please let me know if anyone has a good name for it.
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
- Status changed from Open to Feedback
I'm uncertain about what you mean by "change".
Could you elaborate the behavior what you expect?
Updated by yuki24 (Yuki Nishijima) almost 10 years ago
As you may know, in Ruby 2.2 and older, #local_variables returns a list of local variables available in the scope where the method is called, or raises an NameError if it's called from outside of the class:
class NameError
def call_local_variables
foo = bar = nil
local_variables
end
end
the_value = nil
begin
doesnt_exist
rescue => e
# Ruby 2.2 and older
e.local_variables # => NoMethodError: private method `local_variables' called...
e.call_local_variables # => [:foo, :bar]
end
We've changed it to behave like the following:
... # same setup
begin
doesnt_exist
rescue => e
# Ruby 2.3 (current)
e.local_variables # => [:the_value]
e.call_local_variables # => [:the_value]
end
And an example of what I would like to add/change would be something like:
... # same setup
begin
doesnt_exist
rescue => e
# Ruby 2.4
e.local_variables # => NoMethodError: private method `local_variables' called...
e.call_local_variables # => [:foo, :bar]
e.something_new # => [:the_value]
end
Updated by nobu (Nobuyoshi Nakada) almost 10 years ago
Rename NameError#local_variables not to override Kernel#local_variables?
Do you have candidates for that name?