Project

General

Profile

Actions

Feature #12113

open

Global method inside Delegator causes NameError

Added by oleg_antonyan (Oleg Antonyan) about 8 years ago. Updated about 8 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:74004]

Description

def some_func
  puts '12'
end

class Klass < Delegator
  def initialize(obj)
    @obj = obj
  end

  def __getobj__
    @obj
  end

  def func
    some_func #=> /home/oleg/.rbenv/versions/2.3.0/lib64/ruby/2.3.0/delegate.rb:85:in `instance_method': undefined method `some_func' for module `Kernel' (NameError)
  end
end

Klass.new(0).func

Delegator uses Kernel.instance_method (https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L85) but:

::Kernel.respond_to?(:some_func, true) #=> true
::Kernel.instance_method(:some_func)   #=> `instance_method': undefined method `some_func' for module `Kernel' (NameError)
::Kernel.method(:some_func)            #=> #<Method: Module(Object)#some_func>

I think there should be Kernel.method instead of instance_method (in Delegator). Otherwise you get respond_to? == true, but cannot use this method and get an error.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0