From: 7stud -- Date: 2011-08-23T11:37:03+09:00 Subject: Re: Gritty Details of super() luke gruber wrote in post #1017907: > > Another, sort of related thing: > > Speaking of method lookup, class objects that have singleton classes > embedded in singleton classes look up methods in the superclass's > singleton class's singleton class (if it exists) and actually stop at > the top-most singleton class of the original class object. A mouthful... > I find this really cool if not pretty useless :) > I used that feature once in a solution I posted here. :) The op wanted to a method out of some other object's lookup path, or some such thing. > And like if a class > object has 10 singleton classes with a greet() method defined in the > seventh and eighth, and the subclass calls greet() in its sixth > singleton class, it won't resolve. > Right. A singleton class is just an object. And all objects have a parent class and a singleton class. But the singleton class of a singleton class is not in the first singleton class's lookup path. Here is an example: class Dog def Dog.greet puts 'hello' end end obj = Dog.singleton_class obj.singleton_class.class_eval do def speak puts 'woof' end end Dog.greet Dog.speak --output:-- hello ruby.rb:16:in `
': undefined method `speak' for Dog:Class (NoMethodError) Or equivalently: class Dog class << self def greet puts 'hello' end class << self def speak puts 'woof' end end end end Dog.greet Dog.speak --output:-- hello ruby.rb:18:in `
': undefined method `speak' for Dog:Class (NoMethodError) -- Posted via http://www.ruby-forum.com/.