0

何をしinitializer = new configurationますか?

def self.run(configuration = Configuration.new)
  yield configuration if block_given?
  initializer = new configuration
  initializer.
  initializer
end
4

2 に答える 2

2

これらの行を見てみましょう。

# 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
于 2013-01-12T18:08:05.727 に答える
0

そのコードのコンテキストは、おそらく次のようなものです。

class MyClass
  def self.run
  ....

したがって、self は MyClass です。書き込みは、クラスの新しいインスタンスを単純に作成するためnewのショートカットです。self.newMyClass.new

于 2013-01-12T17:58:44.863 に答える