I am using super to pass arguments to the parent initialize method, which is not called by default. This is what it looks like. (Notice the use of super on the last two arguments)
module Pet
def initialize name, is_pet
@is_pet = is_pet
if is_pet
@name = name
else
@name = "Unnamed"
end
end
def pet?
return @is_pet
end
def get_name
return @name
end
end
class Dog
include Pet
def initialize tricks, name, is_pet
@tricks = tricks
super name, is_pet
end
def get_tricks
return @tricks
end
end
Here's what I can do with it:
d = Dog.new ["roll", "speak", "play dead"], "Spots", true
d.pet? #=> true
d.get_tricks #=> ["roll", "speak", "play dead"]
d.get_name #=> "Spots"
It works fine, but I'm just wondering if there's a better way to do this.
returnas the last value is automatically the return value.