To_a

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

On 5/10/07, Ari B. [email protected] 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?
This depends on the content of line 45 of 122_CreditCards.rb :wink:
Would you mind sharing it (with some context lines) with us?

Cheers
Robert

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 D. wrote:

What should I do instead of to_a?
This depends on the content of line 45 of 122_CreditCards.rb :wink:
Would you mind sharing it (with some context lines) with us?

-------------------------------------------------------|
~ Ari
crap my sig won’t fit

On May 11, 2007, at 7:04 AM, Ari B. wrote:

using the
~ 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.

On 5/10/07, Ari B. [email protected] wrote:

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 D. wrote:

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

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.


Rick DeNatale

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

On 5/11/07, Martin DeMello [email protected] wrote:

a class that doesn’t provide it, so that the implentation defaults to
=> [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/

Hi –

On Sat, 12 May 2007, Rick DeNatale wrote:

What should I do instead of to_a?
So you get the deprecation warning when you call to_a on an object of

[a]
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

On 5/11/07, Ari B. [email protected] 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]

martin

On 5/11/07, [email protected] [email protected] wrote:

Hi –

On Sat, 12 May 2007, Rick DeNatale wrote:

[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
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.


Rick DeNatale

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

Hi –

On Sat, 12 May 2007, Rick DeNatale wrote:

[obj] # => [“already”, “an”, “array”]

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

Whoops :slight_smile:

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

This is an email that will respond to all who are interested in my
misdemeanor among rubists…
On May 11, 2007, at 5:15 PM, Martin DeMello wrote:

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.

a = 1
=> 1

a.to_a
(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!!!

[a]
=> [1]

That’s about five more things I just added to my ruby vocabulary :smiley:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs