# Forum: Ruby indexed array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2007-02-02 08:40
```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```
on 2007-02-02 09:49
```On 2/2/07, Josselin <josselin@wanadoo.fr> 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```
on 2007-02-02 14:32
```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```
This topic is locked and can not be replied to.