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
On Thu, Oct 22, 2009 at 11:24 AM, Mmcolli00 Mom [email protected]
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
On Oct 22, 2009, at 11:37 AM, Gregory B. wrote:
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 B. http://agileconsultingllc.com
[email protected]
On Thu, Oct 22, 2009 at 5:24 PM, Mmcolli00 Mom [email protected]
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,
Jesus.
2009/10/22 Jesús Gabriel y Galán [email protected]:
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.
Jesus.
–
Paul S.
http://www.nomadicfun.co.uk
[email protected]
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.
On Thu, Oct 22, 2009 at 6:07 PM, Paul S. [email protected]
wrote:
=> []
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]
Jesus.
Thanks you so much for your help!!!
MMCOLLI00
On Fri, 23 Oct 2009, [ISO-8859-1] Jesús Gabriel y Galán wrote:
irb(main):001:0> array = []
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]
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
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.
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 Y.'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.
Greg B. wrote:
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 Y.
http://DevMentor.org
Do Good ~ Share Freely