S
Summercool
as we can use
foo ||= Cart.new
so that if foo is nil, then now foo = Cart.new
but let's say if I have a variable i want to increment by 1
is there an idiomatic and elegant way to say
foo += 1, unless foo is nil, then set it to 1 ?
maybe
foo = (foo == nil ? 1 : foo + 1)
but it is kind of long and not readable
foo ||= Cart.new
so that if foo is nil, then now foo = Cart.new
but let's say if I have a variable i want to increment by 1
is there an idiomatic and elegant way to say
foo += 1, unless foo is nil, then set it to 1 ?
maybe
foo = (foo == nil ? 1 : foo + 1)
but it is kind of long and not readable