# Setting nil to zero

**URL:** <https://www.ruby-forum.com/t/setting-nil-to-zero/72172>\
**Category:** Ruby\
**Created:** [February 23, 2006, 3:41am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172 "2006-02-23T03:41:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [February 23, 2006, 3:41am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172/1 "2006-02-23T03:41:35Z")

</div>

Hi,

is there a setting to set nil to zero? something like $NIL=0, then set  
it back again to $NIL=nil…

reason is i have quite a number of codes like ff

s.each do |w|  
f[w] = 0 unless f[w] #i want to kill this lne since it does not jive  
with my pseudocode  
f[w] += 1  
end

thanks and kind regards -botp

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [February 23, 2006, 3:50am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172/2 "2006-02-23T03:50:45Z")

</div>

On 22-Feb-06, at 9:38 PM, Peña, Botp wrote:

> f[w] += 1  
> end
> 
> thanks and kind regards -botp

If I’m doing that with a hash then I use 0 as a default value e.g.

michael-stoks-powerbook-g4-17:~ mike$ irb  
irb(main):001:0\> f = Hash.new(0)  
=\> {}  
irb(main):002:0\> f[7] += 1  
=\> 1  
irb(main):003:0\> f  
=\> {7=\>1}

but I assume that you are after something more subtle…

Michael

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [February 23, 2006, 3:53am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172/3 "2006-02-23T03:53:47Z")

</div>

On Thu, 23 Feb 2006, [iso-8859-1] Peña, Botp wrote:

> thanks and kind regards -botp

why not

f = Hash::new{|h,k| h[k] = 0}  
s.each{|w| f[w] += 1}

??

-a

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [February 23, 2006, 4:08am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172/4 "2006-02-23T04:08:56Z")

</div>

On Feb 22, 2006, at 9:38 PM, Peña, Botp wrote:

> f[w] += 1  
> end
> 
> thanks and kind regards -botp

s.each do |w|  
f[w] = f[w].to\_i + 1 # Or if f[w] can be a float, f[w].to\_f  
end

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [February 23, 2006, 10:20am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172/5 "2006-02-23T10:20:20Z")

</div>

Botp wrote:

> f[w] += 1  
> end

… and if f is not a Hash you can do

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

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [February 23, 2006, 10:49am UTC](https://www.ruby-forum.com/t/setting-nil-to-zero/72172/6 "2006-02-23T10:49:48Z")

</div>

Robert K. wrote:

> Botp wrote:
> 
> > f[w] += 1  
> > end
> 
> … and if f is not a Hash you can do
> 
> f[w] = (f[w] || 0) + 1

You want an integer from Mr. Nil? Just ask him.

f[w] = f[w].to\_i + 1

Cheers,  
Dave
