-1
我想了解在类中包含或继承模块的概念。每当我阅读时,我都会得到新的方法来在课堂中包含或继承一个模块。所以只想知道有多少种方式。下面是实施例中,我共享:在类中包含或继承模块的方法
实施例1
module TimeExtensions
refine Fixnum do
def minutes; self * 60; end
end
end
class MyApp
using TimeExtensions
def initialize
p 2.minutes
end
end
实施例2
VAL = 'Global'
module Foo
VAL = 'Foo Local'
class Bar
def value1
VAL
end
end
end
class Foo::Bar
def value2
VAL
end
end
实施例3
module Foo
def foo
puts 'heyyyyoooo!'
end
end
class Bar
include Foo
end
这三个例子做了非常不同的事情。你可以问这三者之间有什么区别。另一方面,如果你只是想要不同的语法,你可以得到很多,但是有什么意义呢? – ndn
我正在学习如何在课堂中包含一个模块。每当我看到我得到一个不同的方式。所以我的问题是为什么有3种方法做一个小东西.. – ankur
http:// ruby-doc .org/core-2.0.0/Module.html#method-i-prepend是另一种可能性。 –