# 1.upto(4) in ruby 1.8.6

**URL:** <https://www.ruby-forum.com/t/1-upto-4-in-ruby-1-8-6/153102>\
**Category:** Ruby\
**Created:** [November 12, 2008, 7:05am UTC](https://www.ruby-forum.com/t/1-upto-4-in-ruby-1-8-6/153102 "2008-11-12T07:05:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [November 12, 2008, 7:05am UTC](https://www.ruby-forum.com/t/1-upto-4-in-ruby-1-8-6/153102/1 "2008-11-12T07:05:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [November 12, 2008, 7:10am UTC](https://www.ruby-forum.com/t/1-upto-4-in-ruby-1-8-6/153102/2 "2008-11-12T07:10:25Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [November 12, 2008, 7:45am UTC](https://www.ruby-forum.com/t/1-upto-4-in-ruby-1-8-6/153102/3 "2008-11-12T07:45:31Z")

</div>

On Nov 11, 10:13 pm, Peña, Botp [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [November 12, 2008, 7:16am UTC](https://www.ruby-forum.com/t/1-upto-4-in-ruby-1-8-6/153102/4 "2008-11-12T07:16:51Z")

</div>

From: Tom Verbeure [[mailto:removed\_email\_address@domain.invalid](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)

?
