On 5/11/07, Martin DeMello <martindemello / gmail.com> wrote:
> On 5/11/07, Ari Brown <ari / aribrown.com> wrote:
> > Howdy, jewel hunters
> >
> >         I've been working on the Credit Card problem, and  am using the
> > command .to_a to put  my numbers into an array.
> >
> > However, I get a warning everytime I do so:
> > #122_CreditCards.rb:45: warning: default `to_a' will be obsolete
> >
> > What should I do instead of to_a?
>
> Ruby currently implements Object#to_a, which just returns [self]:
>
>   obj.to_a -> anArray
> ------------------------------------------------------------------------
>      Returns an array representation of obj. For objects of class
>      Object and others that don't explicitly override the method, the
>      return value is an array containing self. However, this latter
>      behavior will soon be obsolete.
>
> So you get the deprecation warning when you call to_a on an object of
> a class that doesn't provide it, so that the implentation defaults to
> Object#to_a.
>
> Use [obj] instead:
>
> >> a = 1
> => 1
> >> a.to_a
> (irb):2: warning: default `to_a' will be obsolete
> => [1]
> >> [a]
> => [1]

Not exactly the same thing though, if you don't know that the obj
isn't already an array then there's no guarantee that

[obj] == obj.to_a

obj = 1
[obj] #=> [1]

obj = %{already an array}
[obj] #=> [['already', 'an', 'array']]

That's why the context is important in determining the actuall problem
being addressed.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/