%w{foo bar baz}
#=> ["foo", "bar", "baz"]
Array('foobar')
#=> ["foobar"]
Array(%w(foo bar baz))
#=> ["foo", "bar", "baz"]
Given the method do_something(*args)
, when we call do_something("foo")
, args
will be a String, but when calling do_something("foo", "bar")
, we have an array. Using the above line, we always have an array.
sum = (foo || 0) + bar
sum = (foo or 0) + bar
foo = nil # foo MUST be initialized
# some code, which MAY set foo
foo + 42 #!=> NoMethodError: undefined method `+' for nil:NilClass
42 + foo #!=> TypeError: nil can't be coerced into Fixnum
(foo || 0 ) + 42 #=> 42
||
or or
are boolean expressions. The right-hand part of the expression only gets evaluated, if the left-hand part evaluates to false
(which nil
does!).