是否有一种惯用的方法来为Ruby中的可选参数指定默认值?

时间:2021-09-29 11:01:42

Is there a more concise and idiomatic way to write the following code, which is used to specify default values for optional parameters (in the params/options hash) to a method?

是否有更简洁和惯用的方法来编写以下代码,用于指定方法的可选参数(在params / options哈希中)的默认值?

def initialize(params={})
  if params.has_key? :verbose
    @verbose = params[:verbose]
  else
    @verbose = true # this is the  default value
  end
end

I would love to simplify it to something like this:

我希望将它简化为这样的东西:

def initialize(params={})
  @verbose = params[:verbose] or true
end

which almost works, except that you really need to use has_key? :verbose as the condition, instead of just evaluating params[:verbose], in order to cover cases when you want to specify a value of 'false' (i.e. if you want to pass :verbose => false as the argument in this example).

几乎可以工作,除了你真的需要使用has_key? :详细作为条件,而不是仅仅评估params [:verbose],以便在您想要指定值'false'时覆盖案例(即,如果您想要传递:verbose => false作为此示例中的参数)。

I realize that in this simple example I could easily do:

我意识到在这个简单的例子中我可以很容易地做到:

def initialize(verbose=false)
  @verbose = verbose
end

but, in my real code I actually have a bunch of optional parameters (in addition to a few required ones) and I'd like to put the optional ones in the params hash so that I can easily only specify (by name) the few that I want, rather than having to list them all in order (and potentially having to list ones I don't actually want).

但是,在我的实际代码中,我实际上有一堆可选参数(除了一些必需的参数),我想把可选的参数放在params散列中,这样我就可以轻松地指定(按名称)少数几个我想要的,而不是必须按顺序列出它们(并且可能必须列出我实际上并不想要的那些)。

4 个解决方案

#1


14  

A common pattern is to use

一种常见的模式是使用

def foo(options = {})
  options = { :default => :value }.merge(options)
end

You’ll end up with options being a hash containing the passed in values, with the options from your defaults hash as any that weren’t provided.

您最终会将选项作为包含传入值的哈希,并使用默认哈希中的选项作为未提供的选项。

#2


2  

Ruby 2.0.0 has a new feature keyword arguments

Ruby 2.0.0有一个新的特征关键字参数

Previously you had to write such code:

以前你必须编写这样的代码:

def foo(options = {})
  options = {bar: 'bar'}.merge(options)
  puts "#{options[:bar]} #{options[:buz]}"
end

foo(buz: 'buz') # => 'bar buz'

Now this is much cleaner:

现在这更清洁了:

def foo(bar: 'bar', **options)
  puts "#{bar}, #{options}"
end

foo(buz: 'buz') # => 'bar buz'

#3


0  

I think you're looking for this

我想你正在寻找这个

params = { :verbose => true }.merge(params)

#4


0  

Another way to write this, more succinctly, would be

写这个的另一种方式,更简洁,就是

def foo(options = {})
    options.reverse_merge! value1: true, value2: 100
end

This set options[:value1] to true (default value) unless options passed in already contains the key :value1. Same for :value2

这将选项[:value1]设置为true(默认值),除非传入的选项已包含键:value1。相同:value2

#1


14  

A common pattern is to use

一种常见的模式是使用

def foo(options = {})
  options = { :default => :value }.merge(options)
end

You’ll end up with options being a hash containing the passed in values, with the options from your defaults hash as any that weren’t provided.

您最终会将选项作为包含传入值的哈希,并使用默认哈希中的选项作为未提供的选项。

#2


2  

Ruby 2.0.0 has a new feature keyword arguments

Ruby 2.0.0有一个新的特征关键字参数

Previously you had to write such code:

以前你必须编写这样的代码:

def foo(options = {})
  options = {bar: 'bar'}.merge(options)
  puts "#{options[:bar]} #{options[:buz]}"
end

foo(buz: 'buz') # => 'bar buz'

Now this is much cleaner:

现在这更清洁了:

def foo(bar: 'bar', **options)
  puts "#{bar}, #{options}"
end

foo(buz: 'buz') # => 'bar buz'

#3


0  

I think you're looking for this

我想你正在寻找这个

params = { :verbose => true }.merge(params)

#4


0  

Another way to write this, more succinctly, would be

写这个的另一种方式,更简洁,就是

def foo(options = {})
    options.reverse_merge! value1: true, value2: 100
end

This set options[:value1] to true (default value) unless options passed in already contains the key :value1. Same for :value2

这将选项[:value1]设置为true(默认值),除非传入的选项已包含键:value1。相同:value2