Topic: Setting each_with_index's default value

Hi,

I'm doing something like:

array.each_with_index do |item, i|
  item.update_attribute(:number, (i+1).to_s)
end

Is there any way to set the default value of i to start at 1 instead of 0 so I don't have to do the (i+1) bit?  I'm looking everywhere but can't seem to find it.  Thanks,

Drew

Re: Setting each_with_index's default value

No. Perhaps you should consider numbering your attributes from zero if it bothers you?

Re: Setting each_with_index's default value

specious wrote:

No. Perhaps you should consider numbering your attributes from zero if it bothers you?

Indeed.  dbanning, 0 isn't the default value of anything, it's the exact, specific, value of the index of the first element. 

If you wanted you could shove a dummy value as the first element in the array when you populate it, so that the 'real' elements do actually start at index 1.  But this would probably cause more confusion.  What you have is fine.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams