# Newb question

**URL:** <https://www.ruby-forum.com/t/newb-question/148002>\
**Category:** Ruby\
**Created:** [September 8, 2008, 8:01am UTC](https://www.ruby-forum.com/t/newb-question/148002 "2008-09-08T08:01:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 8:01am UTC](https://www.ruby-forum.com/t/newb-question/148002/1 "2008-09-08T08:01:10Z")

</div>

Why does this

p 3.times {|i| puts i}

produce

0  
1  
2  
3

???

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 8:05am UTC](https://www.ruby-forum.com/t/newb-question/148002/2 "2008-09-08T08:05:51Z")

</div>

Try

p 2.times {|i| puts “Number:#{i}”}

u will see the last line is the returncode of i from the last loop

Rong schrieb:

> ???

–  
Otto Software Partner GmbH

Jan P. (e-mail: removed\_email\_address@domain.invalid)

Tel. 0351/49723202, Fax: 0351/49723119  
01067 Dresden, Freiberger Straße 35 - AG Dresden, HRB 2475  
Geschäftsführer: Burkhard Arrenberg, Heinz A. Bade, Jens Gruhl

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 8:08am UTC](https://www.ruby-forum.com/t/newb-question/148002/3 "2008-09-08T08:08:12Z")

</div>

On Sep 8, 2008, at 12:53 AM, Rong wrote:

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

The loop outputs 0, 1, and 2.

The ‘p’ outputs the value of the entire statement. The times method  
returns the value of its receiver, which is 3 in this case.

Regards

Dave

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 8:26am UTC](https://www.ruby-forum.com/t/newb-question/148002/4 "2008-09-08T08:26:22Z")

</div>

On Sep 8, 2008, at 1:11 AM, Rong wrote:

> But why is it incrementing?

Because you asked it to?

## ri Integer.times ---------------------------------------------------------- Integer#times int.times {|i| block } =\> int

```
  Iterates block int times, passing in values from zero to int - 1.

     5.times do |i|
       print i, " "
     end

  produces:

     0 1 2 3 4
```

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 8:19am UTC](https://www.ruby-forum.com/t/newb-question/148002/5 "2008-09-08T08:19:20Z")

</div>

But why is it incrementing?

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 10:30am UTC](https://www.ruby-forum.com/t/newb-question/148002/6 "2008-09-08T10:30:10Z")

</div>

On Sun, Sep 7, 2008 at 11:11 PM, Rong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But why is it incrementing?

It’s not. n.times {|i| … } calls the block with values from 0 to  
n-1, then returns n. The loop variable isn’t incremented that one  
final time, if that’s what you were asking.

martin

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/newb-question/148002/7 "2008-09-08T15:40:41Z")

</div>

Ron G. wrote:

> Why does this
> 
> p 3.times {|i| puts i}
> 
> produce
> 
> 0  
> 1  
> 2  
> 3
> 
> ???

I think that a way to see what they are saying more clearly is thus:

ar = Array.new  
3.times {|i| ar \<\< i}  
p ar

=\> [0, 1, 2]

It does things as expected but internally to the block it has that last  
bit.

I hope that helps.

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 8, 2008, 10:01am UTC](https://www.ruby-forum.com/t/newb-question/148002/8 "2008-09-08T10:01:58Z")

</div>

On Sun, Sep 7, 2008 at 10:53 PM, Rong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why does this
> 
> p 3.times {|i| puts i}
> 
> produce
> 
> 0  
> 1  
> 2  
> 3

try the following:

a = 3.times {|i| puts i}  
puts “a = #{a}”

martin
