On May 25, 12:23 pm, Rich Morin <r... / cfcl.com> wrote:
> Thanks for the suggestions!  Here's a summary, with comments:
>
> > foo.to_a.each            {|value| ... }
>
>   This is what I was using.  It's nice to know that it's not
>   going away, after all.
>
> > foo.to_a.each            {|value| ... } if foo
>
>   This moves the "if" clause away from the iteration, which
>   might make it harder to see.  Also, it evaluates foo twice
>   (could duplicate side-effects if foo is a method or be a
>   performance issue id evaluating foo is expensive).
>
> > foo and foo.each         {|value| ... }
>
>   A bit wordy and obscure.  Also, it evaluates foo twice.
>
> > class NilClass
> >   def each; self; end
> >   include Enumerable
> > end
>
> > foo.each                 {|value| ... }
>
>   This shortens the usage code, but I don't like playing with
>   the definition of NilClass.  Specifically, this might confuse
>   a human reader or get in the way of code that already expects
>   the current behavior.
>
> > (foo || []).each         {|value| ... }

A very common idiom in Ruby however.

>   This smells like line-noise, but is otherwise quite adequate.
>
> > (foo || VOID).each       {|value| ... }
> > (foo || void).each       {|value| ... }
>
>   Aside from the line-noise issue, these seem similar to me.
>   Both require the use of a supplementary definition, however,
>   which the reader might have to loop up.

This is a hypothetical. The significant advantage of "void" would come
from a core implementation which would be much more efficient than [].
(A "void" object would also have some other good uses however. hint
hint matz ;)

T.