code
创建一个win.rb
文件:
PS C:\Users\Administrator\RubyCode> ls
-a--- 2016/6/3 16:52 142 win.rb
使用prepend M
语句 , module win
:
module M
def hello
puts "Module win!"
end
end
class C
prepend M # output "Module win!"
def hello
puts "Class win."
end
end
c = C.new
c.hello
PS C:\Users\Administrator\RubyCode> ruby win.rb
Module win!
使用include M
语句 ,class win
:
module M
def hello
puts "Module win!"
end
end
class C
include M # "Class win."
def hello
puts "Class win."
end
end
c = C.new
c.hello
PS C:\Users\Administrator\RubyCode> ruby win.rb
Class win.
reference
《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
4.2.3. How prepend works
∧ ∧ 寝坊ダ!
__(;>Д<)_
| ノ ⊃ ⊃ |
|\⌒⌒ ⌒⌒\
| \^ ⌒⌒ ⌒\
\ |⌒⌒⌒⌒⌒ |
\|日曜日ダッタ..
http://emoji.vis.ne.jp/tobiokiru.htm