# Including range error

**URL:** <https://www.ruby-forum.com/t/including-range-error/107003>\
**Category:** Ruby\
**Created:** [June 16, 2007, 11:41am UTC](https://www.ruby-forum.com/t/including-range-error/107003 "2007-06-16T11:41:18Z")\
**Posts on this page:** 2\
**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:** [June 16, 2007, 11:41am UTC](https://www.ruby-forum.com/t/including-range-error/107003/1 "2007-06-16T11:41:18Z")

</div>

I wrote the following command

myRanges = [(0…2), (2…5), (5…10), (10…15), (15…25)]

myRanges.each\_pair {|index, aRange| index if aRange.include?(1)}  
I would like to get 0 as a result since 1 is falling into the first  
range…

but I got an error

undefined method `each\_pair’ for [0…2, 2…5, 5…10, 10…15,  
15…25]:Array

how should I write it, if I want to get the index of the range where a  
given number is falling…

thanks for your help

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:** [June 16, 2007, 11:49am UTC](https://www.ruby-forum.com/t/including-range-error/107003/2 "2007-06-16T11:49:18Z")

</div>

Alle sabato 16 giugno 2007, Josselin ha scritto:

> undefined method `each\_pair’ for [0…2, 2…5, 5…10, 10…15,  
> 15…25]:Array
> 
> how should I write it, if I want to get the index of the range where a  
> given number is falling…
> 
> thanks for your help
> 
> joss

each\_pair is not a method of Array. If you need both the index and the  
element  
in the block, you need to use each\_with\_index, which passes the block  
the  
element and the index. Your code would become:

myRanges.each\_with\_index{|aRange, index| index if aRange.include?(1)}

This removes the error, but doesn’t give the result you expect, because  
each\_with\_index returns the receiver (myRanges), in this case. If you  
want to  
stop the iteration when a range including 1 is found, and return its  
index,  
you need to break the loop explicitly:

myRanges.each\_with\_index{|aRange, index| break index if  
aRange.include(1)}

I hope this helps

Stefano
