[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: Enumerable#zip(aRange) does not work ?
From: Simon Strandgaard <neoneye () adslhome ! dk>
Date: 2004-02-27 13:34:45
Message-ID: pan.2004.02.27.13.29.29.174418 () adslhome ! dk
[Download RAW message or body]
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.
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>
--
Simon Strandgaard
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic