相关疑难解决方法(0)

Ruby - 关键字参数 - 您能将所有关键字参数视为哈希吗?怎么样?

我有一个看起来像这样的方法:

def method(:name => nil, :color => nil, shoe_size => nil) 
  SomeOtherObject.some_other_method(THE HASH THAT THOSE KEYWORD ARGUMENTS WOULD MAKE)
end
Run Code Online (Sandbox Code Playgroud)

对于任何给定的调用,我可以接受任意值的任意组合.我喜欢命名参数,因为我可以查看方法的签名以查看可用的选项.

我不知道的是,如果上面的代码示例中有大写字母描述的快捷方式.

回到过去,它曾经是:

def method(opts)
  SomeOtherObject.some_other_method(opts)
end
Run Code Online (Sandbox Code Playgroud)

优雅,简单,几乎作弊.

这些关键字参数是否有快捷方式,还是我必须在方法调用中重构我的选项哈希?

ruby hash arguments options keyword

30
推荐指数
2
解决办法
6709
查看次数

标签 统计

arguments ×1

hash ×1

keyword ×1

options ×1

ruby ×1