Methods names' type
The method names for instance_methods will be String in 1.8.7 and Symbol since 1.9.1.
Since instance_method(method_name)don't care whether method_name is Symbol or String, this seems to be a innocuous difference, but if you code has something like Klass.instance_methods.include?("f") it will break when changing versions. This is also valid for methods and singleton_methods.
class A; def f; end; end p A.instance_method(:f) #=> #<Method:A#f> p A.instance_method('f') #=> #<Method:A#f> p A.instance_methods(false) #=> Ruby 1.8.7: ["f"] #=> Ruby 1.9.1: [:f]
The superclass of the singleton class of a class
The superclass of the singleton class (also known as eigenclass) of a class X is the singleton class of the superclass of X. This follows the natural way of class methods lookup (not counting the extended modules). However, this works only since 1.9.1. In Ruby 1.8.7, the superclass of the singleton class of any class is the singleton class of the class Class:
class A; end class B < A; end # singleton class of B SB = class << B; self; end p SB #=> #<Class:B> p SB.superclass #=> Ruby 1.8.7: #<Class:Class> #=> Ruby 1.9.1: #<Class:A>
Binding class methods to a subclass
For instance methods, you can bind an unbound method to an object since that object is an instance of the method's class or subclass. For class methods, you can bind an unbound method to a subclass of the method's class (or to the class itself). This works since 1.9.2. Earlier versions raises TypeError if you try to bind a class method to a subclass:
class A def self.f "self is #{self}" end end class B < A; end p A.method(:f).unbind.bind(B).call #=> Ruby until 1.9.1: TypeError #=> Ruby since 1.9.2: "self is B"
super from a module method after binding
In Ruby 1.8.7, if you bind an instance method of a module to an object (of a class which includes that module), and if that method has super, it will raise NoMethodError instead of looking for the super method. It will flow normally if that method is invoked without bind.
class A def f "Hello" end end module M def f super + " world!" end end class B < A include M end b = B.new p b.f #=> "Hello world!" p M.instance_method(:f).bind(b).call #=> Ruby 1.8.7: NoMethodError #=> Ruby 1.9.1: "Hello world!"
No comments:
Post a Comment