From: Philip Hallstrom Date: 2007-09-11T08:34:11+09:00 Subject: Re: is there a one liner to initialize a variable to 1, or increment by 1? > 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 ? foo = foo.to_i + 1 would work as nil.to_i == 0