Topic: how can I build something like :function, :something => true

how can I build a method like that
:function, :something => true

does

def function(options = {})
end

works?

Last edited by jtomasrl (2011-07-22 17:01:08)

Re: how can I build something like :function, :something => true

I'm not sure what you're after but if you have a definition you mention you can call it like that.  However, it's not how Rails usually solves this.  It's:

def function(*args)
  options = args.extract_options!
  options[:something] = false unless options[:something]
  ...
  if options[:something]
     ...
  end
  ...
end

it has quite a few benefits, one being that it's the 'standard' way.

Note: when passing in boolean values decide if you do or do not want to differentiate between false and nil (i.e. no value passed in) and write your defaults set-up and checks appropriately.  In my example it's always true or false.

Re: how can I build something like :function, :something => true

thanks for the explanation