From: Greg Barozzi Date: 2009-10-23T02:26:05+09:00 Subject: Re: convert/replace a value of nil with 0? 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/.