indexed array

J

Josselin

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
 
J

Jan Svitok

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 = 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
 
A

ara.t.howard

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top