# Convert/replace a value of nil with 0?

**URL:** <https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906>\
**Category:** ruby-talk\
**Created:** [22 October 2009 15:24 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906 "2009-10-22T15:24:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [22 October 2009 15:24 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/1 "2009-10-22T15:24:58Z")

</div>

Do you know how I can convert or replace any value that gets back a  
'nil' to 0?

For instance, the myArrayVal[0] may or may not contain a number and I  
want the code to default to 0 if a nil is returned. I have not found any  
documentation on this.

Thanks in advance.  
MMCOLLI00

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [22 October 2009 15:37 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/2 "2009-10-22T15:37:28Z")

</div>

> Do you know how I can convert or replace any value that gets back a  
> 'nil' to 0?
> 
> For instance, the myArrayVal[0] may or may not contain a number and I  
> want the code to default to 0 if a nil is returned. I have not found any  
> documentation on this.

> > nil.to\_i

=\> 0

> > nil.to\_f

=\> 0.0

> **···**
>
> On Thu, Oct 22, 2009 at 11:24 AM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [22 October 2009 15:46 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/3 "2009-10-22T15:46:42Z")

</div>

irb(main):001:0\> array =   
=\>   
irb(main):002:0\> value = array[0] || 0  
=\> 0  
irb(main):003:0\> value  
=\> 0

Hope this helps,

Jesus.

> **···**
>
> On Thu, Oct 22, 2009 at 5:24 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:
> 
> > Do you know how I can convert or replace any value that gets back a  
> > 'nil' to 0?
> > 
> > For instance, the myArrayVal[0] may or may not contain a number and I  
> > want the code to default to 0 if a nil is returned. I have not found any  
> > documentation on this.

---

<div class="post-metadata">

**Author:** ![Greg\_Barozzi](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@Greg\_Barozzi](https://rubytalk.org/u/Greg_Barozzi)\
**Post date:** [22 October 2009 17:26 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/4 "2009-10-22T17:26:05Z")

</div>

Mmcolli00 Mom wrote:

> Do you know how I can convert or replace any value that gets back a  
> 'nil' to 0?
> 
> For instance, the myArrayVal[0] may or may not contain a number and I  
> want the code to default to 0 if a nil is returned. I have not found any  
> documentation on this.

If you want to check each value in an array, try:

myArrayVal.each\_index do |i|  
&nbsp;&nbsp;&nbsp;myArrayVal[i] = 0 if myArrayVal[i].nil?  
end

The each\_index method allows you to iterate through the array and access  
each element specifically.

A less wordy way of writing this would be:

myArrayVal.each\_index do |i|  
&nbsp;&nbsp;&nbsp;myArrayVal[i] ||= 0  
end

But using ||= will also convert a value of FALSE to 0, and that may not  
be a valid operation in your app. Hope this helps and good luck.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_Gleeson](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robert\_Gleeson](https://rubytalk.org/u/Robert_Gleeson)\
**Post date:** [23 October 2009 01:14 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/5 "2009-10-23T01:14:47Z")

</div>

Mmcolli00 Mom wrote:

> Do you know how I can convert or replace any value that gets back a  
> 'nil' to 0?
> 
> For instance, the myArrayVal[0] may or may not contain a number and I  
> want the code to default to 0 if a nil is returned. I have not found any  
> documentation on this.
> 
> Thanks in advance.  
> MMCOLLI00

If you know the size your array is going to be or the maximum size it  
will ever be, you could also try:

arr = Array.new 5, 0  
# =\> [0,0,0,0,0]

Hope this helps,  
Rob.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [22 October 2009 15:45 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/6 "2009-10-22T15:45:58Z")

</div>

And taking Gregory's information a step further,

(myArrayVal[0] || 0)

Or you could '|| 9' if you wanted to default nil to 9.

When you say "may not contain a number", might you possibly find a string there, too?

myArrayVal[0] == 'cat'

Just having you think about it.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Oct 22, 2009, at 11:37 AM, Gregory Brown wrote:
> 
> > On Thu, Oct 22, 2009 at 11:24 AM, Mmcolli00 Mom \> \<mmc\_collins@yahoo.com\> wrote:
> > 
> > > Do you know how I can convert or replace any value that gets back a  
> > > 'nil' to 0?
> > > 
> > > For instance, the myArrayVal[0] may or may not contain a number and I  
> > > want the code to default to 0 if a nil is returned. I have not found any  
> > > documentation on this.
> > 
> > > > nil.to\_i
> > 
> > =\> 0
> > 
> > > > nil.to\_f
> > 
> > =\> 0.0

---

<div class="post-metadata">

**Author:** ![Paul\_Smith1](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@Paul\_Smith1](https://rubytalk.org/u/Paul_Smith1)\
**Post date:** [22 October 2009 16:07 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/7 "2009-10-22T16:07:21Z")

</div>

> > Do you know how I can convert or replace any value that gets back a  
> > 'nil' to 0?
> > 
> > For instance, the myArrayVal[0] may or may not contain a number and I  
> > want the code to default to 0 if a nil is returned. I have not found any  
> > documentation on this.
> 
> irb(main):001:0\> array =   
> =\>   
> irb(main):002:0\> value = array[0] || 0  
> =\> 0  
> irb(main):003:0\> value  
> =\> 0
> 
> Hope this helps,

Though that will of course convert false too.

> **···**
>
> 2009/10/22 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> 
> > On Thu, Oct 22, 2009 at 5:24 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:
> 
> > Jesus.
> 
> --  
> Paul Smith  
> [http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)
> 
> paul@pollyandpaul.co.uk

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [22 October 2009 18:43 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/8 "2009-10-22T18:43:51Z")

</div>

Thanks you so much for your help!!!

MMCOLLI00

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dev\_Guy](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dev\_Guy](https://rubytalk.org/u/Dev_Guy)\
**Post date:** [23 October 2009 00:15 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/9 "2009-10-23T00:15:24Z")

</div>

Greg Barozzi wrote:

> Mmcolli00 Mom wrote:
> 
> > Do you know how I can convert or replace any value that gets back a  
> > 'nil' to 0?
> > 
> > For instance, the myArrayVal[0] may or may not contain a number and I  
> > want the code to default to 0 if a nil is returned. I have not found any  
> > documentation on this.
> 
> If you want to check each value in an array, try:
> 
> myArrayVal.each\_index do |i|  
> &nbsp;&nbsp;&nbsp;myArrayVal[i] = 0 if myArrayVal[i].nil?  
> end
> 
> The each\_index method allows you to iterate through the array and access each element specifically.
> 
> A less wordy way of writing this would be:
> 
> myArrayVal.each\_index do |i|  
> &nbsp;&nbsp;&nbsp;myArrayVal[i] ||= 0  
> end
> 
> But using ||= will also convert a value of FALSE to 0, and that may not be a valid operation in your app. Hope this helps and good luck.

Following the examples, if you want to create a new array, you can also do this:

irb(main):001:0\> a=  
irb(main):002:0\> a[3]=5  
irb(main):003:0\> a[6]=12.4  
irb(main):004:0\> a[7]=22

irb(main):005:0\> p a  
[nil, nil, nil, 5, nil, nil, 12.4, 22]

irb(main):007:0\> b = a.collect { |x| x.nil? ? 0:x }  
irb(main):008:0\> p b  
[0, 0, 0, 5, 0, 0, 12.4, 22]

> **···**
>
> --  
> Kind Regards,  
> Rajinder Yadav
> 
> [http://DevMentor.org](http://DevMentor.org)  
> Do Good ~ Share Freely

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [22 October 2009 16:43 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/10 "2009-10-22T16:43:19Z")

</div>

Sure, if you can have in the array things that are not numbers I think  
to\_i as Gregory suggested should work in general. Or you can do it  
more explicitly like this:

value = array[0].nil? ? 0 : array[0]

Jesus.

> **···**
>
> On Thu, Oct 22, 2009 at 6:07 PM, Paul Smith \<paul@pollyandpaul.co.uk\> wrote:
> 
> > 2009/10/22 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> > 
> > > On Thu, Oct 22, 2009 at 5:24 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:
> > > 
> > > > Do you know how I can convert or replace any value that gets back a  
> > > > 'nil' to 0?
> > > > 
> > > > For instance, the myArrayVal[0] may or may not contain a number and I  
> > > > want the code to default to 0 if a nil is returned. I have not found any  
> > > > documentation on this.
> > > 
> > > irb(main):001:0\> array =   
> > > =\>   
> > > irb(main):002:0\> value = array[0] || 0  
> > > =\> 0  
> > > irb(main):003:0\> value  
> > > =\> 0
> > > 
> > > Hope this helps,
> > 
> > Though that will of course convert false too.

---

<div class="post-metadata">

**Author:** ![Colin\_Bartlett1](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Colin\_Bartlett1](https://rubytalk.org/u/Colin_Bartlett1)\
**Post date:** [25 October 2009 10:26 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/11 "2009-10-25T10:26:29Z")

</div>

More possibilities?

(1) If you just want an array "accessor" without changing the array itself  
and without setting up a new array, then adapting Jesús Gabriel y Galán's post:

class Object  
&nbsp;&nbsp;def nil\_to( value ); self.nil? ? value : self ; end  
end  
a[5] = nil ; a[5].nil\_to( 0 ) #=\> 0  
a[7] = false ; a[7].nil\_to( 0 ) #=\> false

or even:

class Array  
&nbsp;&nbsp;def at\_with\_nil\_to(i, value); (obj = self[i]).nil? ? value : obj; end  
end

(2) If you want to modify the array itself, instead of creating a new array,  
then adapting Rajinder Yadav's post you can do:  
&nbsp;&nbsp;arr.collect! { |obj| obj.nil? ? 0 : obj }  
or use "map!" which is a synonym for "collect!".  
Using "collect!" seems to be faster than using "each\_with\_index"  
and modifying the appropriate elements. Is that what people would expect?  
I ask because for similar things which modified arrays "in place"  
I used to use "each\_with\_index" type code until I understood what  
collect!/map! did.

---

<div class="post-metadata">

**Author:** ![Matt\_Williams](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@Matt\_Williams](https://rubytalk.org/u/Matt_Williams)\
**Post date:** [22 October 2009 17:25 UTC](https://rubytalk.org/t/convert-replace-a-value-of-nil-with-0/55906/12 "2009-10-22T17:25:53Z")

</div>

Or, being very evil......

class Array  
&nbsp;&nbsp;&nbsp;idx = self.instance\_method(:)  
&nbsp;&nbsp;&nbsp;define\_method(:) do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n=idx.bind(self).call(x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return n.nil? ? 0 : n  
&nbsp;&nbsp;&nbsp;end  
end

(this is based off of [Jay Fields' Thoughts: Ruby: Alias method alternative](http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html) )

Matt

> **···**
>
> On Fri, 23 Oct 2009, [ISO-8859-1] Jesús Gabriel y Galán wrote:
> 
> > On Thu, Oct 22, 2009 at 6:07 PM, Paul Smith \<paul@pollyandpaul.co.uk\> wrote:
> > 
> > > 2009/10/22 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> > > 
> > > > On Thu, Oct 22, 2009 at 5:24 PM, Mmcolli00 Mom \<mmc\_collins@yahoo.com\> wrote:
> > > > 
> > > > > Do you know how I can convert or replace any value that gets back a  
> > > > > 'nil' to 0?
> > > > > 
> > > > > For instance, the myArrayVal[0] may or may not contain a number and I  
> > > > > want the code to default to 0 if a nil is returned. I have not found any  
> > > > > documentation on this.
> > > > 
> > > > irb(main):001:0\> array =   
> > > > =\>   
> > > > irb(main):002:0\> value = array[0] || 0  
> > > > =\> 0  
> > > > irb(main):003:0\> value  
> > > > =\> 0
> > > > 
> > > > Hope this helps,
> > > 
> > > Though that will of course convert false too.
> > 
> > Sure, if you can have in the array things that are not numbers I think  
> > to\_i as Gregory suggested should work in general. Or you can do it  
> > more explicitly like this:
> > 
> > value = array[0].nil? ? 0 : array[0]
