Zack
Zack

Reputation: 2208

Calling methods from module in class

How should I fill the fruit class so that the below code works? I am not able to figure out how to call the printArgument method in Fruit class

module MyModule
  def printArgument(fruit)
    print "from MyModule, printArgument: "
    puts "argument supplied is #{fruit}"
  end
end


class Fruit
  #FILL ME OUT 


end


s = Fruit.new(10)
s.printArgument s.weight

Upvotes: 1

Views: 33

Answers (1)

Drenmi
Drenmi

Reputation: 8777

This implements Fruit:

class Fruit
  include MyModule

  attr_accessor :weight

  def initialize(weight)
    @weight = weight
  end
end

The line include MyModule extends the class with the behaviour of MyModule. This is what is known as a mix-in. The rest merely creates a property weight and makes it publicly accessible.

Upvotes: 2

Related Questions