# Integer.step is inclusive?!

**URL:** <https://www.ruby-forum.com/t/integer-step-is-inclusive/156277>\
**Category:** Ruby\
**Created:** [December 23, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277 "2008-12-23T20:45:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [December 23, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277/1 "2008-12-23T20:45:57Z")

</div>

I don’t know how to post the output of irb so I’ll just have to describe  
it but if I run the following code:

0.upto(10){ |i| puts i }

```
...I will get 11 lines of output. This iterates eleven times. 

```

Really?!  
Why it does this is obvious. Integer.upto is inclusive so it  
includes  
both 0 and 10. However, am I the only one who thinks this is strange  
behaviour? I mean, the ususal programming protocol is that the last  
element  
is not included (half-open sets, if you will) so the number of  
iterations is  
the difference between numbers.  
Why did Ruby turn out this way? Thank you…

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [December 23, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277/2 "2008-12-23T20:56:29Z")

</div>

Just Another Victim of the Ambient M. wrote:

> 0.upto(10){ |i| puts i }
> 
> …IÂ willÂ getÂ 11Â linesÂ ofÂ output

Because 0 to 10 is 11 numbers. If you want 10 numbers, use 1.upto(10)  
If you want different behavior, you can use a different looping method,  
or use post or pre increment, depending if you want it to display the  
last element or not.

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [December 23, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277/3 "2008-12-23T20:58:49Z")

</div>

On Dec 23, 2008, at 2:36 PM, Just Another Victim of the Ambient  
Morality wrote:

> Why it does this is obvious. Integer.upto is inclusive so it  
> includes  
> both 0 and 10. However, am I the only one who thinks this is strange  
> behaviour? I mean, the ususal programming protocol is that the last  
> element  
> is not included (half-open sets, if you will) so the number of  
> iterations is  
> the difference between numbers.  
> Why did Ruby turn out this way? Thank you…

It isn’t really that Ruby is this way just that the method ‘upto’ is  
this way.  
There are several other ways to iterate over an integer range:

> > (0…4).each { |i| puts i }  
> > 0  
> > 1  
> > 2  
> > 3  
> > 4

> > (0…4).each { |i| puts i }  
> > 0  
> > 1  
> > 2  
> > 3

> > 4.times { |i| puts i }  
> > 0  
> > 1  
> > 2  
> > 3

> > 0.step(4).each { |i| puts i }  
> > 0  
> > 1  
> > 2  
> > 3  
> > 4

> > 0.step(4,2).each { |i| puts i }  
> > 0  
> > 2  
> > 4

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [December 23, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277/4 "2008-12-23T21:20:29Z")

</div>

“Just Another Victim of the Ambient M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote  
in message news:xab4l.3997$removed\_email\_address@domain.invalid…

> element is not included (half-open sets, if you will) so the number of  
> iterations is the difference between numbers.  
> Why did Ruby turn out this way? Thank you…

```
D'oh!
...and by ".upto" I, of course, meant ".step" !

```

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [December 23, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277/5 "2008-12-23T21:01:10Z")

</div>

2008/12/23 Just Another Victim of the Ambient M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I don’t know how to post the output of irb so I’ll just have to describe  
> it but if I run the following code:
> 
> 0.upto(10){ |i| puts i }
> 
> …I will get 11 lines of output. This iterates eleven times. Really?!

because this are eleven numbers: 0 1 2 3 4 5 6 7 8 9 10

> Why it does this is obvious. Integer.upto is inclusive so it includes  
> both 0 and 10. However, am I the only one who thinks this is strange  
> behaviour?

why do you think a loop from 0 to 10 should exclude the upper border?

In Ada it is ‘for i in 0 … 10 loop’ which is a loop from 0 to 10.

-Thomas

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [December 24, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/integer-step-is-inclusive/156277/6 "2008-12-24T14:00:26Z")

</div>

Thomas P. wrote:

> > Why it does this is obvious. Integer.upto is inclusive so it includes  
> > both 0 and 10. However, am I the only one who thinks this is strange  
> > behaviour?
> 
> why do you think a loop from 0 to 10 should exclude the upper border?
> 
> In Ada it is ‘for i in 0 … 10 loop’ which is a loop from 0 to 10.
> 
> -Thomas

To show my age I can say that FORTRAN is inclusive to:

```
    DO 100 I = 0,10

```

…  
100 CONTINUE

executes 11 times.

Many languages I know don’t have looping structures of this sort but  
have explicit initialization, test, post loop action things (e.g.  
C/C++ for loops). There it’s idiomatic to write e.g.:

for(i =0; i \< 10; i++) {…}

since int array[10] has elements 0…9, however it’s certainly not  
required as  
for(i =0; i\<=10; i++){…}  
is certainly just fine… if that’s what’s intended.

I go back to my mantra when using a language I’m new to. The mantra  
is based on a ‘game’ model of programming languages… languages are  
games, they have rules. The rules may have reasons, or they may be  
arbitrary choices. They may even be capricious in some languages  
(e.g. LOLCODE see  
[http://www.globalnerdy.com/2007/05/28/lolcode-the-lolcat-programming-language/](http://www.globalnerdy.com/2007/05/28/lolcode-the-lolcat-programming-language/)).

But if you don’t play by the rules, you lose. If you do you might  
win. Asking for the rules to be different, doesn’t help you win the  
game … so the mantra

Ruby is not xxxxx

where you can substitute for xxxxx the set of languages you are trying  
to force ruby to be.

Ron
