Hi gurus and nubys,
I just noticed that enumerable#zip just accepts an Array as argument:
puts (1…10).zip(21…30)
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.
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.
···
On 2004-02-27 06:34:48 +0900, gabriele renzi wrote:
puts (1…10).zip(21…30)
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?
–
="puts’=‘+.inspect+‘;’+";puts’='+.inspect+‘;’+_
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>
···
On Fri, 27 Feb 2004 22:19:29 +0900, Florian From wrote:
On 2004-02-27 06:34:48 +0900, gabriele renzi wrote:
puts (1…10).zip(21…30)
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.
–
Simon Strandgaard
Simon Strandgaard wrote:
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?
···
On Fri, 27 Feb 2004 22:38:36 +0900, Emmanuel Touzery wrote:
–
Simon Strandgaard