Why is to_a going to be obsolete?

J

Joel VanderWerf

daz said:
:

Don't get too fond of it ;)

irb(main):016:0> [~] irb
irb(main):001:0> h = {1=>2, :three=>"four"}
=> {1=>2, :three=>"four"}
irb(main):002:0> [*h]
=> [[1, 2], [:three, "four"]]
irb(main):003:0> class << h
irb(main):004:1> undef :to_a
irb(main):005:1> end
=> nil
irb(main):006:0> h.to_a
NoMethodError: undefined method `to_a' for {1=>2, :three=>"four"}:Hash
from (irb):6
irb(main):007:0> [*h]
NoMethodError: undefined method `to_a' for {1=>2, :three=>"four"}:Hash
from (irb):7


I don't think the future is so gloomy :)
Hash#to_a access is blocked by your code as well as Object#to_a.

Quite right. The example shouldn't have been a hash, since Hash#to_a
isn't (AFAIK) going away any time soon. A better example:

irb(main):001:0> [*Class]
=> [Class]
irb(main):002:0> class Object; undef :to_a; end
=> nil
irb(main):003:0> [*Class]
NoMethodError: undefined method `to_a' for Class:Class
from (irb):3

IMHO, the future is not made gloomy by this change :)
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Why is to_a going to be obsolete?"

|My apology, I read a few more messages: to_a will still be the
|sanctioned way for an object to emit itself as an array, but Object
|will not include such a method. Correct?

Right, as long as I understand you correctly.

matz.
 
G

Gavin Sinclair

I thought that was the purpose of to_ary()?

#to_x and #to_xyz are meant to have different semantics (explicit and
implicit conversion, respectively). So there is room for both #to_a
and #to_ary in Enumerable et al.

Gavin
 

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

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top