1.upto(4) in ruby 1.8.6


#1

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


#2

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


#3

On Nov 11, 10:13 pm, Peña, Botp removed_email_address@domain.invalid wrote:

?
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


#4

From: Tom Verbeure [mailto:removed_email_address@domain.invalid]

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)

?