Project

General

Profile

Feature #12042

A better interface that returns a list of local variables available where the exception is raised

Added by yuki24 (Yuki Nishijima) over 1 year ago. Updated over 1 year ago.

Status:
Feedback
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:73629]

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.

History

#1 [ruby-core:73632] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Status changed from Open to Feedback

I'm uncertain about what you mean by "change".
Could you elaborate the behavior what you expect?

#2 [ruby-core:73648] Updated by yuki24 (Yuki Nishijima) over 1 year 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

#3 [ruby-core:73663] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

Rename NameError#local_variables not to override Kernel#local_variables?

Do you have candidates for that name?

Also available in: Atom PDF