Hi --

On Thu, 24 Aug 2006, Mauricio Fernandez wrote:

> On Thu, Aug 24, 2006 at 01:45:40AM +0900, James Edward Gray II wrote:
>> On Aug 23, 2006, at 11:11 AM, Nathan Smith wrote:
>>
>>> I think he's wanting to know if it's possible to call a different  method
>>> of the superclass than the method that the interpreter is in.
>>
>> I'm not sure it's a great idea, but sure you can:
>>
>> def send_super(meth, *args, &blk)
>>   # hide current method
>>   if self.class.instance_methods(false).include? meth.to_s
>>     self.class.send(:alias_method, :_hidden, meth)
>>     self.class.send(:remove_method, meth)
>>   end
>>
>>   send(meth, *args, &blk)
>> ensure
>>   self.class.send(:alias_method, meth, :_hidden) if methods.include? "_hidden"
>> end
>
> wow, that's way too much work (plus thread-unsafe).
>
> def send_super(meth, *args, &b)
>  self.class.superclass.instance_method(meth).bind(self).call(*args, &b)
> end
>
>
> RUBY_VERSION                                       # => "1.8.5"
> RUBY_RELEASE_DATE                                  # => "2006-07-07"
> class Parent
>  def a
>    "Hello from Parent!"
>  end
> end
>
> class Child < Parent
>  def a
>    "Hello from Child!"
>  end
>
>  def b
>    send_super(:a)
>  end
> end
>
> child = Child.new
> puts child.b
> puts child.a
>
> __END__
> # >> Hello from Parent!
> # >> Hello from Child!

This only works for superclasses, I think, whereas super just looks
higher up in the method lookup chain, in modules as well as classes.

I'm thinking of, for example:

class A
   def x
     puts "A#x"
   end
end

module M
   def x
     puts "M#x"
   end
   def y
     send_super(:x)
   end
end

a = A.new
a.extend(M)

a.x
a.y

So... maybe this:

# Sigh -- Matz, *please* can we have this? :-)
def singleton_class
   class << self; self; end
end

def send_super(meth, *args, &b)
   m = singleton_class.ancestors[1..-1].find {|a| a.instance_methods(false).include?(meth.to_s)}
   m.instance_method(meth).bind(self).call(*args, &b)
end


David

-- 
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black     => book, Ruby for Rails
http://www.rubycentral.org       => Ruby Central, Inc.