On 10/30/07, Martin DeMello <martindemello / gmail.com> wrote:
> On 10/30/07, Josselin <josselin / wanadoo.fr> wrote:
> > I have a group of lines  (used in Googlemaps display)  based on 2 arrays :
> >
> > zl = 9 if farthest_proposition.distance_to(origin) < = 25.0
> > zl = 10 if farthest_proposition.distance_to(origin) <= 15.0
> > zl = 11 if farthest_proposition.distance_to(origin) <= 10.0
> > zl = 12 if farthest_proposition.distance_to(origin) <= 5.0
> > zl = 13 if farthest_proposition.distance_to(origin) < =2.0
> > zl = 14 if farthest_proposition.distance_to(origin) <= 1.0
> >
> > how could I replace it the dryest way possible ?
> >
> > zl = [9, 10, 11, 12, 13, 14]
> > distance = [1.0, 2.0, 5.0, 10.0, 15.0, 25.0]
>
> s = farthest_proposition.distance_to(origin)
> zl = distance.zip(zl.reverse) {|d, z| break [z,d] if s <= d}

oops, that should be break z, of course, not break [z,d]

The basic idea is this: the block form of as.zip(bs) {|a, b|} iterates
over as and bs in parallel, and break argument both exits the current
iterator and returns its argument as the return value of the iterator.

martin