From: Rob Biedenharn Date: 2009-10-23T00:45:58+09:00 Subject: Re: convert/replace a value of nil with 0? On Oct 22, 2009, at 11:37 AM, Gregory Brown wrote: > On Thu, Oct 22, 2009 at 11:24 AM, 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. > >>> 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 Rob@AgileConsultingLLC.com