Hi,
I can’t seem to get it right with a super (newbie stuff :)).
I expect the following program :
···
class Boo
def say_what
puts "boo what ?"
end
end
a = Boo.new
a.say_what
class Foo < Boo
def say_what
super.say_what
puts "FOO this!"
end
end
b = Foo.new
b.say_what
to output
boo what?
boo what?
FOO this!
Instead I get :
boo what ?
on ruby 1.7.2 (2002-03-29) [i686-cygwin]
and
boo what ?
boo what ?
class_object_vars.rb:12:in say_what': undefined method
say_what’ for nil
(NoMethodError)
from class_object_vars.rb:18
on ruby 1.7.2 (2002-03-20) [i386-mswin32]
class_object_vars.rb:12: being the super.say_what line…
Could someone please clear me out why is this happening ?
Thanks,
philip
I can’t seem to get it right with a super (newbie stuff :)).
:
class Foo < Boo
def say_what
super.say_what
puts “FOO this!”
end
end
:
Could someone please clear me out why is this happening ?
To quote matz from ruby-talk:38327
···
On Tue, Jun 04, 2002 at 03:37:07AM +0900, Philip Mateescu wrote:
it should be super(). Forget your Smalltalk (or Objective-C)
experience about “super”.
–
marko schulz
Thank you Tobias and thank you Marko.
However, what if I would want to call another method of its ancestor ?
Something along the lines of :
class Boo
def say_what
puts “boo what?”
def
def say_who
puts “class Boo”
end
end
class Foo < Boo
def say_what
# none of the following three work
super.say_who # xor
super().say_who # xor
super.send :say_who #
I do have an extra question (TIA) : why does it (Foo.new.say_what) still
prints “boo what?” in all three cases ?
Does it executes super (thus Boo#say_what) and then gets lots in which is
the current object ?
Thanks,
philip
···
----- Original Message -----
From: “Tobias Reif” tobiasreif@pinkjuice.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, 03 June, 2002 9:48 PM
Subject: Re: super bug or (most likely) I am doing something wrong ?
Philip Mateescu wrote:
Tobi
–
http://www.pinkjuice.com/
“Philip Mateescu” philip@dynasty.com wrote in message news:010e01c20b36$51276130$0a01a8c0@DynRom.local…
Thank you Tobias and thank you Marko.
However, what if I would want to call another method of its ancestor ?
Something along the lines of :
class Boo
def say_what
puts “boo what?”
def
def say_who
puts “class Boo”
end
end
class Foo < Boo
def say_what
# none of the following three work
super.say_who # xor
super().say_who # xor
super.send :say_who #
I do have an extra question (TIA) : why does it (Foo.new.say_what) still
prints “boo what?” in all three cases ?
super # <== looks for say_what in superclass
so, your example is calling the Boo.say_what, then trying to send a
say_who message to the returned value. also, does do what you want?
class Foo < Boo
alias say_what say_who
end
hope this was helpful, I’m a little bleary eyed right now.
night,
Patrick