to_a

A

Ari Brown

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?


-------------------------------------------------------|
~ Ari
crap my sig won't fit
 
R

Robert Dober

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?
This depends on the content of line 45 of 122_CreditCards.rb ;)
Would you mind sharing it (with some context lines) with us?

Cheers
Robert
 
A

Ari Brown

Ok, I seemed to have fixed it. .to_a works, but what I need was .split
(//). I never knew until now that it puts it into an array for you!

This depends on the content of line 45 of 122_CreditCards.rb ;)
Would you mind sharing it (with some context lines) with us?

-------------------------------------------------------|
~ Ari
crap my sig won't fit
 
J

John Joyce

Ok, I seemed to have fixed it. .to_a works, but what I need
was .split(//). I never knew until now that it puts it into an
array for you!



-------------------------------------------------------|
~ Ari
crap my sig won't fit
Ari, warnings about deprecations are just that. Warnings. They ARE
true. In the future these things will be removed and their
replacements already exist. Sometimes they are just synonyms and
sometimes they are actually different implementations. Any time you
see such a warning just try to find the method that should be used
instead ( ri is a good way to check this, there is usually some blurb
telling you the alternative method.)
Generally, Matz is slow about completely removing that functionality
to give time for people to change and refactor old code. But don't
depend on it.
 
R

Rick DeNatale

Ok, I seemed to have fixed it. .to_a works, but what I need was .split
(//). I never knew until now that it puts it into an array for you!

On May 10, 2007, at 6:01 PM, Robert Dober wrote:

Ari,

You're still too mysterious about what you fixed. If you show the
line which caused the original problem, we might help you get a deeper
understanding of what's really going on.
 
M

Martin DeMello

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:
(irb):2: warning: default `to_a' will be obsolete
=> [1]
=> [1]

martin
 
R

Rick DeNatale

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:
(irb):2: warning: default `to_a' will be obsolete
=> [1]
=> [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.
 
D

dblack

Hi --

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]
=> [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)
 
R

Rick DeNatale

Hi --

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']]
I think that the * operator will (always?) do it:

[*1] # => [1]
[obj] # => ["already", "an", "array"]

Yep, as long as you actually USE it <G>
irb(main):001:0> obj = %w{already an array}
=> ["already", "an", "array"]
irb(main):002:0> [*obj]
=> ["already", "an", "array"]

I'm not sure whether or not the to_splat changes in Ruby 1.9 will have
a subtle effect on this though.
 
D

dblack

Hi --

Hi --

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']]
I think that the * operator will (always?) do it:

[*1] # => [1]
[obj] # => ["already", "an", "array"]

Yep, as long as you actually USE it <G>
irb(main):001:0> obj = %w{already an array}
=> ["already", "an", "array"]
irb(main):002:0> [*obj]
=> ["already", "an", "array"]

Whoops :)
I'm not sure whether or not the to_splat changes in Ruby 1.9 will have
a subtle effect on this though.

I thought that was considered just an experiment... and I admit I had
sort of hoped that was the case. But if it does remain, it will
definitely make my answer obsolete.


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)
 
A

Ari Brown

This is an email that will respond to all who are interested in my
misdemeanor among rubists...
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:
Well that certainly seems to do the trick.

This is exactly what I did.
(irb):2: warning: default `to_a' will be obsolete
=> [1]

It produced an error, but I was unsure of what to make of it. I knew
that it would be obsoletified, and I thought there was some
enumerator function that most people did. I didn't, and still don't,
know how to use enumerators in this, or what they can even be fully
used for. Help?

Apparently .split(//) puts things into an array. Did you know
that?!?! I didn't know that!!!


That's about five more things I just added to my ruby vocabulary :-D

--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top