Forget this old way and say hello to the true keyword arguments in Ruby 2.0. Using the same example as above, here is how we can define our config method in the new way:1
def config(enabled: true, timeout: 300)
2
[enabled, timeout]
3
end
复制代码Now, let us see the different ways in which we can invoke this method:1
> config() #no args
2
#=> [true, 300]
3
> config(enabled: false) #only enabled
4
#=> [false, 300]
5
> config(timeout: 20) #only timeout
6
#=> [true, 20]
7
> config(timeout: 10, enabled: false) #inverse order
8
#=> [false, 10]
复制代码 |