# Setting nil to zero

**URL:** <https://rubytalk.org/t/setting-nil-to-zero/25168>\
**Category:** ruby-talk\
**Created:** [23 February 2006 10:19 UTC](https://rubytalk.org/t/setting-nil-to-zero/25168 "2006-02-23T10:19:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [23 February 2006 10:19 UTC](https://rubytalk.org/t/setting-nil-to-zero/25168/1 "2006-02-23T10:19:24Z")

</div>

Robert Klemme [mailto:bob.news@gmx.net] :

#f[w] = (f[w] || 0) + 1

clear rubyish solution, Robert.  
thanks again for generous help.

i am still hoping for the plain f[w] += 1 though 🙂

for the record, i played w the ff

> cat test.rb

class NilClass  
&nbsp;&nbsp;&nbsp;def method\_missing(methId, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str = methId.id2name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if str == "+" and args[1].nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#str is "+"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#args[1] is nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#args[0] is rhs value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return args[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

f = {}  
f["test"] += 1  
f["test2"] += 2  
p f

f =   
f[1] += 1  
f[5] += 2  
p f

> ruby test.rb

{"test2"=\>2, "test"=\>1}  
[nil, 1, nil, nil, nil, 2]

I doubted if it's robust enough, so my q...

-botp

#Kind regards

> **···**
>
> #  
> # robert  
> #  
> #  
> #

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [23 February 2006 10:53 UTC](https://rubytalk.org/t/setting-nil-to-zero/25168/2 "2006-02-23T10:53:42Z")

</div>

Botp wrote:

> Robert Klemme [mailto:bob.news@gmx.net] :
> 
> #f[w] = (f[w] || 0) + 1
> 
> clear rubyish solution, Robert.  
> thanks again for generous help.
> 
> i am still hoping for the plain f[w] += 1 though 🙂

As mentioned already you can do that, if it's a Hash:

> > f=Hash.new 0

=\> {}

> > f[:foo]+=1

=\> 1

> > f

=\> {:foo=\>1}

> for the record, i played w the ff
> 
> \>cat test.rb
> 
> class NilClass  
> &nbsp;&nbsp;&nbsp;def method\_missing(methId, \*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str = methId.id2name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if str == "+" and args[1].nil?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#str is "+"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#args[1] is nil  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#args[0] is rhs value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return args[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> end

Why so complicated? Why not just

class NilClass  
&nbsp;&nbsp;def +(n) n end  
end

> > h={}

=\> {}

> > h[:foo]+=1

=\> 1

> > h

=\> {:foo=\>1}

Note, I recommend to use the veriant with 0 as default value for Hash  
lookups because that avoids tampering with standard classes.

Cheers

&nbsp;&nbsp;&nbsp;&nbsp;robert
