Project

General

Profile

Actions

Feature #12042

closed

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

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

Added by yuki24 (Yuki Nishijima) almost 10 years ago. Updated almost 10 years ago.

Status:
Feedback
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.

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago Actions #1 [ruby-core:73632]

  • 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 Actions #2 [ruby-core:73648]

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 Actions #3 [ruby-core:73663]

Rename NameError#local_variables not to override Kernel#local_variables?

Do you have candidates for that name?

Actions

Also available in: PDF Atom