1.upto(4) in ruby 1.8.6

T

Tom Verbeure

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
 
P

Peña, Botp

From: Tom Verbeure [mailto:[email protected]]=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)

?
 
T

Tom Verbeure

From: Tom Verbeure [mailto:[email protected]]
# 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
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top