Green Eggs And Ham, super

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 methodeat’ 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!

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!

looks like you’ve got it…

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 methodeat’ 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs