# Determining if array elements are in a range

**URL:** <https://www.ruby-forum.com/t/determining-if-array-elements-are-in-a-range/136101>\
**Category:** Rails\
**Created:** [April 28, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/determining-if-array-elements-are-in-a-range/136101 "2008-04-28T22:49:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [April 28, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/determining-if-array-elements-are-in-a-range/136101/1 "2008-04-28T22:49:03Z")

</div>

So, we’ve got this little problem, that we know how to solve but we  
think that there might be a better way of doing it, so here we go.

We have an array of number, let’s say something like

[29, 34, 58, 91, 102, 107, 110, 136, 145, 154, 167, 226, 239, 240,  
251, 261, 268, 271, 290, 305, 320, 350, 505, 521, 529, 533, 544, 583,  
596, 598, 618, 620, 665, 687, 725, 779, 780, 795, 857, 858, 900, 1013,  
1130, 1163, 1336, 1356, 1416, 1480, 1500, 1950, 2300, 3450, 3509,  
4400, 5300, 7300, 7600, 14000]

We need to know if the numbers are in certain ranges, so for example,  
does the array contain at least one number in the following ranges

0 to 500  
501 to 1000  
1001 to 5000

now we think the solution is something like this (let’s say the array  
of numbers is called values)

ranges = [[0,500],[501, 1000], [1001, 5000]]  
range\_found = {:0 =\> false, :501 =\> false, :1001 =\> false}  
current\_position = 0

for range in ranges

```
while (values[current_position] < range[0] ||

```

values[current\_position] \> range[0]) && current\_position \<  
values.length  
current\_position = current\_position + 1  
end

```
range_found[range[0].to_sym] = current_position < values.length

```

end

Anyone have any idea on how to make this simpler?

Dale

---

<div class="post-metadata">

**Author:** ![PeteSalty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bc79bd/32.png) [@PeteSalty](https://www.ruby-forum.com/u/PeteSalty)\
**Post date:** [April 28, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/determining-if-array-elements-are-in-a-range/136101/2 "2008-04-28T23:03:07Z")

</div>

Hi –

On Mon, 28 Apr 2008, PeteSalty wrote:

> 4400, 5300, 7300, 7600, 14000]
> 
> end
> 
> range\_found[range[0].to\_sym] = current\_position \< values.length
> 
> end
> 
> Anyone have any idea on how to make this simpler?

The first thing to do is use the Range class, rather than reinventing  
it 🙂 So maybe something like this:

ranges = [0…500, 501…1000, 1001…5000]  
results = {}  
values.each do |v|  
results[v] = ranges.any? {|r| r.include?(v) }  
end

You could also use inject to similar ends:

results = values.inject({}) {|hash, v| ranges.any? {|r| r.include?(v)  
} }

I"m not sure why you’re doing a #to\_sym operation on the numbers. You  
can reintroduce it if you want. It seems easier just to use the  
numbers.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
INTRO TO RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
