Hi --

On Fri, 29 Aug 2008, Patrick Doyle wrote:

>> Even better, if its possible you'd want to use the method on a structure
>> other than an array in the future, use 'respond-to?' instead.  So for
>> instance, if you're protecting a call to 'each':
>>
>> if x.respond_to? :each
>>  x.each do
>>    ...
>>  end
>> end
>>
>> That way, it'll work not only with Arrays, but also with Lists and
>> Hashes or any other Enumerable class (or any class that implements
>> each).  So if you later on decide that it would make more sense to store
> except...
>
> #each doesn't always mean the same thing to everybody.
>
> For Hashes, it means "each key and value pair" for arrays, it means
> "each value, but without the index".  On my TODO list is to figure out
> what (virtual) paperwork is required to propose the addition of an
> #each_element method that _would_ mean the same thing to everybody.
> (For Arrays, it would be aliased to #each, for Hashes, it would be
> aliased to #each_value, for a hypothetical List class, it would be
> aliased to some iterator that yields the next element in the list.

"Element" doesn't have a universal meaning either, though. It might be
better to think of normalizing things at the calling level:

   do_something_each_based(hash.values)

etc.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!