Forum: Ruby 1.upto(4) in ruby 1.8.6

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Tom Verbeure (Guest)
on 2008-11-12 08:05
(Received via mailing list)

I'm doing the following:

indices     = 1.upto(points.size)
zipped =, 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?

Tom Verbeure (Guest)
on 2008-11-12 08:10
(Received via mailing list)
> 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]

Peña, Botp (Guest)
on 2008-11-12 08:16
(Received via mailing list)
From: Tom Verbeure [mailto:removed_email_address@domain.invalid]
# indices     = 1.upto(points.size)
# zipped =, points)


but arrays have builtin indices, why not

Tom Verbeure (Guest)
on 2008-11-12 08:45
(Received via mailing list)
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 =, 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] }

This topic is locked and can not be replied to.