10.4 Changing the outputs to include names


#1

Hi everyone,

This exercise was an easy one, but I'm trying to change it up a little bit and having difficulties. The code given by CA is below, and the outputs for both peter and jiminy are "I jumped forward x feet!"

What I'd like to have happen is for the method jump to change depending on which class it is acting on, such that instead of saying "I jumped forward x feet!" it says, when it is called from the rabbit class by the line peter.jump for example "Peter the Rabbit jumped forward x feet!". I don't want to have to redefine the jump function for each class (seems to miss the point of the exercise) but don't quite know how I would edit the output of the jump method from the module inside each of the classes such that in that particular class, we get a different output. Can anyone help? (I tried to do what we did before with classes and subclasses and use a gsub on the super but I think I got ahead of myself.)

module Action
def jump
@distance = rand(4) + 2
puts "I jumped forward #{@distance} feet!"
end
end

class Rabbit
include Action
attr_reader :name
def initialize(name)
@name = name
end
end

class Cricket
include Action
attr_reader :name
def initialize(name)
@name = name
end
end

peter = Rabbit.new("Peter")
jiminy = Cricket.new("Jiminy")

peter.jump
jiminy.jump

Thanks in advance!