initializer = new configuration
是做什麼用的?「初始化程序=新配置」是做什麼的?
def self.run(configuration = Configuration.new)
yield configuration if block_given?
initializer = new configuration
initializer.
initializer
end
initializer = new configuration
是做什麼用的?「初始化程序=新配置」是做什麼的?
def self.run(configuration = Configuration.new)
yield configuration if block_given?
initializer = new configuration
initializer.
initializer
end
代碼的情況下大概是這樣的:
class MyClass
def self.run
....
所以自我是MyClass的。寫作new
是self.new
的快捷方式,意思是MyClass.new
,它只是創建一個新類的實例。
讓我們一起來看看這些路線。
# It is a definition of a class method which takes one argument
# with a default value.
def self.run(configuration = Configuration.new)
# It passes the argument to a block if one was given.
yield configuration if block_given?
# It calls method `new` passing the `configuration` as
# an argument. The returned value is saved in the local
# variable.
initializer = new configuration
# Two following lines are a single expression: a call
# to method `initializer` of the object pointed to by the
# variable `initializer`, i.e.
#
# initializer.send :initializer
#
# No idea why one would break this expression into two lines.
# The value returned from the call to `initializer` becomes
# the return value of the analysed method.
initializer.
initializer
end
'+ 1'由我解釋! –
感謝您的詳細回覆。 :) –
非常感謝!最後我意識到了這個用法。 –