Ruby有个特性,你可以随意打开一个class来添加你需要的方法,类似于下面的情况:
class A
end

class A
   def hello
      p  “hello a”
   end
end



这样A的对象就多了一个hello方法。也许这样满足你的需求,可是你注意到没有,you've made a change to class A.  

所以高手Jay Field建议我们,不要这么做。
he said :   Use modules instead of adding behavior directly.

class B
end

module Bextend
   def hello
      p  “hello b”
   end
end

B.send :include,Bextend

B.new.hello  
#=>   “hello b”

B.ancestors
#=> [B, Bextend, Object, Kernel]


如果你直接opening class去添加你的行为,会增加耦合性(个人理解,欢迎高手指点), 而使用module,会更加灵活。

灵活在哪呢?

好,当你使用opening class的方式添加了一个方法:
class Object
  def hello
    "hello"
  end
end

那么现在给你个任务:change the hello method to include the original behavior and add a name.

你怎么做呢 ? 只能这么做了吧:
class Object
  alias old_hello hello
  def hello(name)
    "#{old_hello} #{name}"
  end
end


但是,如果你使用module来代替opening class的方式定义方法:
class Object
  module Hello
    def hello
      "hello"
    end
  end
  include Hello
end

Object.ancestors
#=> [Object, Hello, Kernel]


你要完成上面的任务只需要:
class Object
  module HelloName
    def hello(name)
      "#{super()} #{name}"
    end
  end
  include HelloName
end

hello("Ali")
# => "hello Ali"
Object.ancestors
=> [Object, HelloName, Hello, Kernel]


上面的例子, 如果你想调用老的hello方法,只需要个super就够了。