Hello!
I try a very simple scheme where the descendent of a class adds
functionality to a method, but somehow I cannot get ‘super’ to work:
class GreenEggs
def eat
<<___
Green Eggs…
end
end
class GreenEggsAndHam < GreenEggs
def eat
super.eat # nope, in eat': undefined method
eat’ for "Green
Eggs…\n":String (NoMethodError)
super eat # nope, stack level too deep (SystemStackError)
<<___
…And Ham!
end
end
puts GreenEggsAndHam::new.eat
OUTPUT
…And Ham!
The desired output is:
Green Eggs…
…And Ham!
datura
2
class GreenEggs
def eat()
<<___
Green Eggs…
end
end
class GreenEggsAndHam < GreenEggs
def eat()
super +
<<___
…And Ham!
end
end
puts GreenEggsAndHam::new.eat
OUTPUT
Green Eggs…
…And Ham!
datura
3
looks like you’ve got it…
datura
4
Agent M. wrote in post #1005169:
Hello!
I try a very simple scheme where the descendent of a class adds
functionality to a method, but somehow I cannot get ‘super’ to work:
class GreenEggs
def eat
<<___
Green Eggs…
end
end
class GreenEggsAndHam < GreenEggs
def eat
super.eat # nope, in eat': undefined method
eat’ for "Green
super – passes all the current method’s args to the super class’s
method with the same name
super() – calls super class’s method with the same name, sending no
args
super(a, b) – calls super class’s method with the same name, sending
the specified args