Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chrisroos/fc62b8e0ce30288abba6a3762b2cb1ee to your computer and use it in GitHub Desktop.
Save chrisroos/fc62b8e0ce30288abba6a3762b2cb1ee to your computer and use it in GitHub Desktop.

I've been investigating why the behaviour of ClassMethod#restore_original_method is different between Ruby 1.8.x and all others versions. See line 77 of ClassMethod.

It turns out that there appears to be a problem in 1.8.7 with using define_method when the method argument is a singleton method. This can be illustrated with the following code:

class Foo
  class << self
    def my_class_method
      'Foo.my_class_method'
    end
  end
end

class Bar < Foo; end

p Foo.my_class_method #=> 'Foo.my_class_method'
p Bar.my_class_method #=> 'Foo.my_class_method'

class Foo
  M = method(:my_class_method)

  class << self
    define_method :my_class_method, M
  end
end

p Foo.my_class_method #=> 'Foo.my_class_method'
p Bar.my_class_method #=> singleton method bound for a different object (TypeError)

Other reports of this behaviour:

It's not clear whether this was a bug that's now been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment