Convert/replace a value of nil with 0?

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/.

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:

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.

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|
   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|
   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/\.

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/\.

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

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

paul@pollyandpaul.co.uk

Thanks you so much for your help!!!

MMCOLLI00

···

--
Posted via http://www.ruby-forum.com/.

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|
   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|
   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
Do Good ~ Share Freely

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.

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
  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
  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:
  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.

Or, being very evil......

class Array
   idx = self.instance_method(:)
   define_method(:) do |x|
     n=idx.bind(self).call(x)
     return n.nil? ? 0 : n
   end
end

(this is based off of Jay Fields' Thoughts: Ruby: Alias method alternative )

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]