Project

General

Profile

Actions

Feature #11777

closed

Change NameError#local_variables to return the list of local variables where the method is raised

Added by yuki24 (Yuki Nishijima) about 9 years ago. Updated about 9 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:71855]

Description

Sasada-san and I talked about this briefly a few weeks ago, but I also wanted to let others know about this.

This change will make it possible to pull out the list of local variables where the exception is raised without TracePoint. The did_you_mean gem uses TracePoint, and the current code looks like this:

# lib/did_you_mean.rb
TracePoint.new(:raise) do |tp|
  e = tp.raised_exception

  if SPELL_CHECKERS.include?(e.class.to_s) && !e.instance_variable_defined?(:@frame_binding)
    e.instance_variable_set(:@frame_binding, tp.binding)
  end
end.enable

# lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb
def initialize(exception)
  ...
  @lvar_names = exception.frame_binding.local_variables

  ...
end

If we change NameError#local_variables as described it'll look like this:

# lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb
def initialize(exception)
  @lvar_names = exception.local_variables

  ...

end

The problem with TracePoint is that it's still a little buggy (also see #11668, #11667) and also makes Ruby slower as reported by Sasada-san. I would like to change the behaviour of NameError#local_variables and remove the use of TracePoint from the gem entirely so we can make the gem much more stable.

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

Do you want to add NameError#local_variables?

Updated by matz (Yukihiro Matsumoto) about 9 years ago

It's OK to add. But I think we should mark it as 'internal use' in the reference.

Matz.

Actions #3

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Status changed from Open to Closed

Applied in changeset r52942.


error.c: name_err_local_variables

  • error.c (name_err_local_variables): new method
    NameError#local_variables for internal use only.
    [Feature #11777]

Updated by znz (Kazuhiro NISHIYAMA) about 9 years ago

NEWS does not mention about this feature.
Is it intended?

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

Yes, this method is only for did_you_mean gem.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0