A TEXT POST

Setting default local variables value in Ruby

Complementing this post from Ruby Quicktips

If you want to ensure that a local variable has a value, you can do so:

>> somevar
NameError: undefined local variable or method `somevar' for main:Object
    from (irb):1
>> somevar = somevar || "somevalue"
=> "somevalue"

but if you try to set another variable, using a value of a variable that may not exists, you will get an error:

>> somevar
NameError: undefined local variable or method `somevar' for main:Object
    from (irb):1
>> someothervar = somevar || "somevalue"
NameError: undefined local variable or method `somevar' for main:Object
    from (irb):2
  1. rafaelss posted this