From: Jamis Buck Date: 2004-01-06T13:20:59+09:00 Subject: Re: Calling shadowed methods in an inherited object Charles Comstock wrote: >If I have code like: > >class A > def foo > puts "A::foo" > end >end > >class B < A > def foo > puts "B::foo" > end > def bar > # call A::foo > end >end > >Is there a legal way to call A::foo from B::bar? > >I would have thought that super gave access to the scope of the parent for >function calls, but I guess it only acts this way for methods named the same as >the current caller. So is there a way todo something like > >super.foo #-> "A::foo" > >from bar? > >I thought I read somewhere that you could but it's possible i am confusing ruby >with another language. > >Thanks, > >Charlie > > There is a way, and it's ugly, but it goes something like this: def bar A.instance_method( :foo ).bind( self ).call end In other words, you get the unbound version of 'foo' from A, bind it to the current instance of B, and invoke it. If there is a cleaner way, I don't know it, but would love to hear about it. -- Jamis Buck jgb3@email.byu.edu ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'