Topic: Syntax to set a variable unless nil

def do_something(options = {})
  prefix = "small_"
  prefix = options[:prefix] if options[:prefix]
end

The above is my long hand way of doing it, is there a better way?


Mnay thanks!

Re: Syntax to set a variable unless nil

prefix = options[:prefix] || "small_"

but it's mostly expressed as :

options[:prefix] ||= "small_"

Last edited by auguste (2007-07-17 09:07:58)

Re: Syntax to set a variable unless nil

Many thanks!