[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

range index

Josselin

12/28/2006 1:54:00 PM

is there any dry way to get the index of an element in a range
without converting the range into an array ?

("A".."Z").each do | alpha |
-> puts alpha.index (?) or something like that .... <-
end

3 Answers

dblack

12/28/2006 2:20:00 PM

0

Josselin

12/28/2006 3:05:00 PM

0

On 2006-12-28 15:19:43 +0100, dblack@wobblini.net said:

> Hi --
>
> On Thu, 28 Dec 2006, Josselin wrote:
>
>> is there any dry way to get the index of an element in a range
>> without converting the range into an array ?
>>
>> ("A".."Z").each do | alpha |
>> -> puts alpha.index (?) or something like that .... <-
>> end
>
> ("A".."Z").each_with_index do |alpha,i|
> puts i
> end
>
>
> David

thanks a lot.. another part of my book to read before year end ;-)))
all 'each_ .... ' statements

joss

Robert Klemme

12/28/2006 6:32:00 PM

0

On 28.12.2006 16:05, Josselin wrote:
> On 2006-12-28 15:19:43 +0100, dblack@wobblini.net said:
>
>> Hi --
>>
>> On Thu, 28 Dec 2006, Josselin wrote:
>>
>>> is there any dry way to get the index of an element in a range
>>> without converting the range into an array ?
>>>
>>> ("A".."Z").each do | alpha |
>>> -> puts alpha.index (?) or something like that .... <-
>>> end
>>
>> ("A".."Z").each_with_index do |alpha,i|
>> puts i
>> end
>>
>>
>> David
>
> thanks a lot.. another part of my book to read before year end ;-)))
> all 'each_ .... ' statements
>

Here's another one

irb(main):005:0> require 'enumerator'
=> true
irb(main):010:0> (1..10).to_enum(:each_with_index).find {|a,b| a==2}[1]
=> 1

robert