0
如何块参数意味着卡皮斯特拉诺获取功能?卡皮斯特拉诺获取块
fetch(:release_path) { current_path }
此外,它可以默认调用?
fetch(:release_path, 'default') { current_path }
如何块参数意味着卡皮斯特拉诺获取功能?卡皮斯特拉诺获取块
fetch(:release_path) { current_path }
此外,它可以默认调用?
fetch(:release_path, 'default') { current_path }
块和第二个参数都用于提供默认值。
例如:
# 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