[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Enumerable#zip(aRange)  does not work ?
From:       Emmanuel Touzery <emmanuel.touzery () wanadoo ! fr>
Date:       2004-02-27 13:38:36
Message-ID: 403F48D5.10807 () wanadoo ! fr
[Download RAW message or body]

Simon Strandgaard wrote:

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


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic