I am new to Ruby and I did the following:
c = {}
# Some code in the middle
c['a'] = c['a'] or 0
Now I would expect this to work like this: if c['a'] is nil, then c['a'] or 0 would return 0. So the value of c['a'] should be 0.
However the value of c['a'] is nil. Why does this happen?
oris not the same as||in Ruby. What you have becomes grouped like so:which will basically do nothing. You should use
||instead:or simply:
Note that
and/&&has the same behavior asor/||.