gouf
3/11/2014 - 2:47 AM

モジュールとインスタンス変数について

モジュールとインスタンス変数について

module Greeting
  def hello name
    @name = name
    "Hello #{name}"
  end
  def name
    @name
  end
end
class Hello
  include Greeting
end
h = Hello.new
puts h.hello('John') #=> Hello John
puts h.name          #=> John
module Greeting
  def self.hello name
    @name = name
    "Hello #{name}"
  end
  def self.name
    @name
  end
end
puts Greeting.hello('John') #=> Hello John
puts Greeting.name          #=> John