Capistrano获取函数中block param的含义如何?
fetch(:release_path) { current_path }
还可以默认调用它吗?
fetch(:release_path, 'default') { current_path }
答案 0 :(得分:1)
块和第二个参数都用于提供默认值。
例如:
# If :some_var is not set, then the default is used
fetch(:some_var) { "default" }
=> "default"
fetch(:some_var, "default")
=> "default"
# Once :some_var is set, the defaults are ignored
set(:some_var, "value")
fetch(:some_var) { "default" }
=> "value"
fetch(:some_var, "default")
=> "value"
您不应该同时指定第二个参数和一个块。在这种情况下,参数将被忽略,并且将使用该块。
# Don't do this, it is confusing
fetch(:another_var, "arg_default") { "block_default" }
=> "block_default"
选择一种形式而不是另一种形式的原因取决于默认值的类型。如果默认值是硬编码的(如上例中的文字字符串),那么参数形式是有意义的。另一方面,如果默认值是计算值(即方法调用),则最好使用块。
Capistrano的fetch
的默认值行为模仿Ruby内置Hash#fetch
的行为,此处记录了这些行为:http://ruby-doc.org/core-2.4.0/Hash.html#method-i-fetch