# Is there a one liner to initialize a variable to 1, or incre

**URL:** <https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299>\
**Category:** Ruby\
**Created:** [September 11, 2007, 1:31am UTC](https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299 "2007-09-11T01:31:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 1:31am UTC](https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299/1 "2007-09-11T01:31:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 1:35am UTC](https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299/2 "2007-09-11T01:35:00Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 2:06am UTC](https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299/3 "2007-09-11T02:06:04Z")

</div>

On Sep 10, 4:34 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> foo = foo.to\_i + 1
> 
> would work as nil.to\_i == 0

I think it was also in Perl that $foo++ will work if $foo is not  
defined, then it will be just treated as 0.  
but if “use strict” then it won’t work… but the code is more robust  
that way

so Ruby is like it will impose the strict for code robustness…

---

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 10:04am UTC](https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299/4 "2007-09-11T10:04:50Z")

</div>

On Sep 11, 2007, at 2:05 AM, Summercool wrote:

> On Sep 10, 4:34 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > foo = foo.to\_i + 1
> > 
> > would work as nil.to\_i == 0
> 
> I think it was also in Perl that $foo++ will work if $foo is not  
> defined, then it will be just treated as 0.  
> but if “use strict” then it won’t work…

It works with strictness on as well. You need to declare the  
variable, that’s the bit strict.pm adds here:

$ perl -Mstrict -wle ‘my $foo; ++$foo; print $foo’  
1

See, $foo is undef when it is incremented.

– fxn

---

<div class="post-metadata">

**Author:** ![Summercool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@Summercool](https://www.ruby-forum.com/u/Summercool)\
**Post date:** [September 11, 2007, 1:43am UTC](https://www.ruby-forum.com/t/is-there-a-one-liner-to-initialize-a-variable-to-1-or-incre/114299/5 "2007-09-11T01:43:00Z")

</div>

On Sep 10, 5:25 pm, Summercool [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 ?

Usually I am doing this sort of thing in a Hash or Array, where the  
default value is your friend:  
counts = Hash.new(0) # or Array.new(0), depending on need  
…later…  
counts[my\_var] += 1

But, to answer your question specifically, I’d probably do a non-one-  
liner:  
foo ||= 0  
foo += 1  
or (like yours, if I was feeling cheeky):  
foo = foo.nil? ? 1 : foo + 1
