3

Possible Duplicate:
Assigning a substitute value in case of nil

In Lua I use

x = value or "default_if_value_is_nil"

(as a shortcut for if value ~= nil then x = value end)

Can I do something similar in Ruby?

Community
  • 1
  • 1
Bożena
  • 39
  • 4

3 Answers3

5
x = value || "default_if_value_is_nil_or_false"

Note the "or false" here, though the same probably holds in Lua too.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Wow, that was easy. What's the difference between || and or in Ruby then? – Bożena Dec 30 '12 at 21:51
  • 3
    Precedence. `or` has a very low precedence, so `a = b or c` is interpreted as `(a = b) or c`. – Thomas Dec 30 '12 at 21:51
  • 1
    @Bożena The precedence is different, see [Using `and` & `or` in Ruby](http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/) and [Difference between `or` and `||` in Ruby](http://stackoverflow.com/questions/2083112/difference-between-or-and-in-ruby). – Andrew Marshall Dec 30 '12 at 21:52
  • Many thanks! But this precedence is unusual, in most languages assignment has the lowest precedence. – Bożena Dec 30 '12 at 21:55
  • 2
    Like so many things in Ruby, it's borrowed from Perl: http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity – Thomas Dec 30 '12 at 21:56
2

You can actually do the same thing in ruby

x = nil_value || "default"

Note that this will also work for any other "falsey" value as well

x = false_value || "default"
ceyko
  • 4,822
  • 1
  • 18
  • 23
2
x = value or "default_if_value_is_nil"

is a perfectly legal Ruby statement, but be aware that or has one of the lowest priority in operators precedence. Also know that in a Ruby boolean operation everything is true except false and nil, thus this Ruby statement will also answer the default even if value is not nil but false.

puts '>>> assignment = has a higher priority than or <<<'
value = 'y'
x = value or "default_if_value_is_nil"
print 'value=', value.inspect, ', x=', x.inspect, "\n"

value = false
x = value or "default_if_value_is_nil"
print 'value=', value.inspect, ', x=', x.inspect, "\n"

value = nil
x = value or "default_if_value_is_nil"
print 'value=', value.inspect, ', x=', x.inspect, "\n"

puts '>>> put parenthesis around or expression to have a higher priority than = <<<'
value = 'y'
x = (value or "default_if_value_is_nil")
print 'value=', value.inspect, ', x=', x.inspect, "\n"

value = false
x = (value or "default_if_value_is_nil")
print 'value=', value.inspect, ', x=', x.inspect, "\n"

value = nil
x = (value or "default_if_value_is_nil")
print 'value=', value.inspect, ', x=', x.inspect, "\n"

puts '>>> || has a higher priority than = <<<'
value = 'y'
x = value || "default_if_value_is_nil"
print 'value=', value.inspect, ', x=', x.inspect, "\n"

value = false
x = value || "default_if_value_is_nil"
print 'value=', value.inspect, ', x=', x.inspect, "\n"

value = nil
x = value || "default_if_value_is_nil"
print 'value=', value.inspect, ', x=', x.inspect, "\n"

Output:

>>> assignment = has a higher priority than or <<<
value="y", x="y"
value=false, x=false
value=nil, x=nil
>>> put parenthesis around or expression to have a higher priority than = <<<
value="y", x="y"
value=false, x="default_if_value_is_nil"
value=nil, x="default_if_value_is_nil"
>>> || has a higher priority than = <<<
value="y", x="y"
value=false, x="default_if_value_is_nil"
value=nil, x="default_if_value_is_nil"
user1852994
  • 223
  • 1
  • 6