Enumerable#zip(aRange) does not work ?

G

gabriele renzi

Hi gurus and nubys,
I just noticed that enumerable#zip just accepts an Array as argument:

TypeError: cannot convert Range into Array

I wonder why this happens: would'nt make much more sense if it just
applied to_ary to it's argument?

note that ri 1.8a (I know, utdated) agrees with me:

(Enumerable#zip)
Converts any arguments to arrays, then merges elements of enumObj
with corresponding elements from each argument.

:)
 
F

Florian From

TypeError: cannot convert Range into Array

I wonder why this happens: would'nt make much more sense if it just
applied to_ary to it's argument?

The problem is that Range doesn't define a to_ary method to convert it
to an array. (And that is probably the correct behaviour because ranges
can be very large.) A possible fix would be to check if an argument of
zip has a to_a method and call that method instead of/additional to
the to_ary call.
 
S

Simon Strandgaard

The problem is that Range doesn't define a to_ary method to convert it
to an array. (And that is probably the correct behaviour because ranges
can be very large.) A possible fix would be to check if an argument of
zip has a to_a method and call that method instead of/additional to
the to_ary call.

Why isn't usage of 'to_a' and 'to_ary' consistent?

irb(main):001:0> class Range
irb(main):002:1> alias to_ary to_a
irb(main):003:1> end
=> nil
irb(main):004:0> (1..3).to_ary
=> [1, 2, 3]
irb(main):005:0> (1..3).to_ary.zip(4..6)
=> [[1, 4], [2, 5], [3, 6]]
irb(main):006:0>
 
E

Emmanuel Touzery

Simon said:
The problem is that Range doesn't define a to_ary method to convert it
to an array. (And that is probably the correct behaviour because ranges
can be very large.) A possible fix would be to check if an argument of
zip has a to_a method and call that method instead of/additional to
the to_ary call.

Why isn't usage of 'to_a' and 'to_ary' consistent?

irb(main):001:0> class Range
irb(main):002:1> alias to_ary to_a
irb(main):003:1> end
=> nil
irb(main):004:0> (1..3).to_ary
=> [1, 2, 3]
irb(main):005:0> (1..3).to_ary.zip(4..6)
=> [[1, 4], [2, 5], [3, 6]]
irb(main):006:0>
to_a is for manual conversion, that you call yourself (explicit conversion)
to_ary is called automatically by the runtime if necessary (implicit
conversion).

it's the same for to_s and to_str.

emmanuel
 
S

Simon Strandgaard

Simon said:
Why isn't usage of 'to_a' and 'to_ary' consistent?
[snip]
to_a is for manual conversion, that you call yourself (explicit conversion)
to_ary is called automatically by the runtime if necessary (implicit
conversion).

it's the same for to_s and to_str.

I wasn't aware of this convention.

Is there any documents where I can read more about this?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top