convert/replace a value of nil with 0?

M

Mmcolli00 Mom

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
 
G

Gregory Brown

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
 
R

Rob Biedenharn

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

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
(e-mail address removed)
 
J

Jesús Gabriel y Galán

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,

Jesus.
 
P

Paul Smith

2009/10/22 Jes=FAs Gabriel y Gal=E1n said:
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 =3D []
=3D> []
irb(main):002:0> value =3D array[0] || 0
=3D> 0
irb(main):003:0> value
=3D> 0

Hope this helps,

Though that will of course convert false too.




--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
J

Jesús Gabriel y Galán

2009/10/22 Jes=FAs Gabriel y Gal=E1n said:
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 an= y
documentation on this.

irb(main):001:0> array =3D []
=3D> []
irb(main):002:0> value =3D array[0] || 0
=3D> 0
irb(main):003:0> value
=3D> 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 =3D array[0].nil? ? 0 : array[0]

Jesus.
 
G

Greg Barozzi

Mmcolli00 said:
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 = 0 if myArrayVal.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 ||= 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.
 
R

Rajinder Yadav

Greg said:
Mmcolli00 said:
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 = 0 if myArrayVal.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 ||= 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
 
R

Robert Gleeson

Mmcolli00 said:
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.
 
C

Colin Bartlett

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=FAs Gabriel y Gal=E1n=
's post:

class Object
def nil_to( value ); self.nil? ? value : self ; end
end
a[5] =3D nil ; a[5].nil_to( 0 ) #=3D> 0
a[7] =3D false ; a[7].nil_to( 0 ) #=3D> false

or even:

class Array
def at_with_nil_to(i, value); (obj =3D self).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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top