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.
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.
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?
snip
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.
Gah that’s nasty. You can get the superclass automatically with