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) over 6 years ago. Updated over 6 years ago.

Status:
Closed
Priority:
Normal
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.

Actions

Also available in: Atom PDF