# Indexed array

**URL:** <https://www.ruby-forum.com/t/indexed-array/84206>\
**Category:** Ruby\
**Created:** [February 2, 2007, 8:40am UTC](https://www.ruby-forum.com/t/indexed-array/84206 "2007-02-02T08:40:58Z")\
**Posts on this page:** 3\
**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:** [February 2, 2007, 8:40am UTC](https://www.ruby-forum.com/t/indexed-array/84206/1 "2007-02-02T08:40:58Z")

</div>

presently I am using : zl = 13-[2,5,10,15,25].index(last\_city\_km)  
when last\_city\_km is one of these fixed values to get a zoom level,

now, there is a change in customer requirement, last\_city\_km is  
calculated, so  
last\_city\_km can be an integer value between 0 and 25,  
and I would like to calculate (DRY) the zoom level such as :

last\_city\_km can be 0

if last\_city\_km \<= 2 then zl = 13  
if 2 \< last\_city\_km \<= 5 then zl = 12  
if 5 \< last\_city\_km \<= 10 then zl = 12  
if 10 \< last\_city\_km \<= 15 then zl = 12  
if 15 \< last\_city\_km \<= 25 then zl = 12

if 25 \< last\_city\_km… cannot !

thanks for

---

<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:** [February 2, 2007, 9:49am UTC](https://www.ruby-forum.com/t/indexed-array/84206/2 "2007-02-02T09:49:04Z")

</div>

On 2/2/07, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if 2 \< last\_city\_km \<= 5 then zl = 12  
> if 5 \< last\_city\_km \<= 10 then zl = 11  
> if 10 \< last\_city\_km \<= 15 then zl = 10  
> if 15 \< last\_city\_km \<= 25 then zl = 9
> 
> if 25 \< last\_city\_km… cannot !
> 
> thanks for

# 13 - [2,5,10,15,25].select{|i| last\_city\_km \<= i}.size - 1

14 - [2,5,10,15,25].select{|i| last\_city\_km \<= i}.size

---

<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:** [February 2, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/indexed-array/84206/3 "2007-02-02T14:32:30Z")

</div>

On Fri, 2 Feb 2007, Josselin wrote:

> if 2 \< last\_city\_km \<= 5 then zl = 12  
> if 5 \< last\_city\_km \<= 10 then zl = 12  
> if 10 \< last\_city\_km \<= 15 then zl = 12  
> if 15 \< last\_city\_km \<= 25 then zl = 12
> 
> if 25 \< last\_city\_km… cannot !
> 
> thanks for

if you think you might end up adding even more logic you might consider  
something like:

harp: ~\> cat a.rb  
require ‘yaml’

[2, 5, 10, 15, 25].each do |last\_city\_km|  
base = 13

zl = base - case last\_city\_km  
when 0 … 2; 0  
when 2 … 5; 1  
when 5 … 10; 2  
when 10 … 15; 3  
when 15 … 25; 4  
else; raise RangeError, last\_city\_km.inspect  
end

y last\_city\_km =\> zl  
end

harp: ~\> ruby a.rb  
2: 13  
5: 12  
10: 11  
15: 10  
25: 9

regards.

-a
