相关疑难解决方法(0)

使用带有默认值的选项哈希作为ruby中的参数是一种很好的干净方法

假设我想要一个像这样调用的方法:

 tiger = create_tiger( :num_stripes => 12, :max_speed => 43.2 )
 tiger.num_stripes # will be 12
Run Code Online (Sandbox Code Playgroud)

其中一些选项具有默认值:

 tiger = create_tiger( :max_speed => 43.2 )
 tiger.num_stripes # will have some default value
Run Code Online (Sandbox Code Playgroud)

在方法实现中实现默认行为的一种很好的惯用ruby方法是什么?

ruby hash

8
推荐指数
4
解决办法
5266
查看次数

标签 统计

hash ×1

ruby ×1