[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

1.upto(4) in ruby 1.8.6

Tom Verbeure

11/12/2008 6:04:00 AM

Hi,

I'm doing the following:

indices = 1.upto(points.size)
zipped = indices.zip(distances, points)

Basically, I'm temporarily annotating my point objects with an index
to avoid losing track of its original position.

indices is just a list going from 1 to points.size.

This works find with ruby 1.8.7, but not for ruby 1.8.6, where upto()
requires a block.

What's the most rubyesque way of doing the same thing in 1.8.6?

Thanks,
Tom

3 Answers

Tom Verbeure

11/12/2008 6:10:00 AM

0


> What's the most rubyesque way of doing the same thing in 1.8.6?

Got it! I just discovered .to_a:

(1..4).to_a => [1,2,3,4]

Tom

Peña, Botp

11/12/2008 6:14:00 AM

0

From: Tom Verbeure [mailto:hombre@gmail.com]=20
# indices =3D 1.upto(points.size)
# zipped =3D indices.zip(distances, points)

(1..points.size).zip(distances,points)

but arrays have builtin indices, why not

distances.zip(points)

?

Tom Verbeure

11/12/2008 6:40:00 AM

0

On Nov 11, 10:13 pm, Peña, Botp <b...@delmonte-phil.com> wrote:
> From: Tom Verbeure [mailto:hom...@gmail.com]
> # indices     = 1.upto(points.size)
> # zipped = indices.zip(distances, points)
>
>   (1..points.size).zip(distances,points)
>
> but arrays have builtin indices, why not
>
>   distances.zip(points)
>
> ?

In later steps, the list gets sorted by the 'distance' field, then
truncated and then sorted back in the original order, but with some
inside points removed.
Like this:

indices = (1..points.size).to_a
zipped = indices.zip(distances, points)
zipped = zipped.delete_if { |x| x[1].nil? }
zipped.sort! { |x,y| y[1] <=> x[1] }
zipped = zipped.slice(0, max_nr_points)

# Sort back in original order
zipped.sort! { |x,y| x[0] <=> y[0] }
points = zipped.collect { |p| p[2] }

Tom