From: dblack@... Date: 2007-05-12T07:35:41+09:00 Subject: Re: to_a Hi -- On Sat, 12 May 2007, Rick DeNatale wrote: > On 5/11/07, Martin DeMello wrote: >> On 5/11/07, Ari Brown 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. I think that the * operator will (always?) do it: [*1] # => [1] [obj] # => ["already", "an", "array"] David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)