# Range index

**URL:** <https://www.ruby-forum.com/t/range-index/80533>\
**Category:** Ruby\
**Created:** [December 28, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/range-index/80533 "2006-12-28T14:55:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 28, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/range-index/80533/1 "2006-12-28T14:55:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 28, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/range-index/80533/2 "2006-12-28T15:21:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 28, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/range-index/80533/3 "2006-12-28T16:12:04Z")

</div>

On 2006-12-28 15:19:43 +0100, removed\_email\_address@domain.invalid said:

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [December 28, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/range-index/80533/4 "2006-12-28T19:35:20Z")

</div>

On 28.12.2006 16:05, Josselin wrote:

> > > ```
> > > -> puts alpha.index (?) or something like that .... <-
> > > 
> > > ```
> 
> 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
```
