Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an idiomatic way to specify default values for optional parameters in Ruby?

Tags:

idioms

ruby

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?

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).

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).

like image 965
Kelan Avatar asked Dec 06 '22 04:12

Kelan


2 Answers

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.

like image 129
Ciarán Walsh Avatar answered Dec 28 '22 10:12

Ciarán Walsh


Ruby 2.0.0 has a new feature keyword arguments

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'
like image 29
freemanoid Avatar answered Dec 28 '22 08:12

freemanoid