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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Be465f8fd9663742d2de6e92fb2d97cb?d=identicon&s=25 Tom Verbeure (Guest)
on 2008-11-12 07:05
(Received via mailing list)
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
Be465f8fd9663742d2de6e92fb2d97cb?d=identicon&s=25 Tom Verbeure (Guest)
on 2008-11-12 07: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]

Tom
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-12 07:16
(Received via mailing list)
From: Tom Verbeure [mailto:hombre@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)

?
Be465f8fd9663742d2de6e92fb2d97cb?d=identicon&s=25 Tom Verbeure (Guest)
on 2008-11-12 07:45
(Received via mailing list)
On Nov 11, 10:13 pm, Peña, Botp <b...@delmonte-phil.com> 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
This topic is locked and can not be replied to.