# Simple method and block. foo.times

**URL:** <https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991>\
**Category:** Ruby\
**Created:** [December 13, 2007, 9:18am UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991 "2007-12-13T09:18:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 13, 2007, 9:18am UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991/1 "2007-12-13T09:18:21Z")

</div>

Why does the output stop at eight. Shouldn’t the class of nine be  
shown also?

9.times {|x| print x , " ", x.class, “\n” }  
0 Fixnum  
1 Fixnum  
2 Fixnum  
3 Fixnum  
4 Fixnum  
5 Fixnum  
6 Fixnum  
7 Fixnum  
8 Fixnum  
=\> 9

–

Regards,

John M.  
MSc (DIC)  
+44 7739 171 531

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 13, 2007, 9:55am UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991/2 "2007-12-13T09:55:51Z")

</div>

Alle giovedì 13 dicembre 2007, John M. ha scritto:

> 6 Fixnum  
> 7 Fixnum  
> 8 Fixnum  
> =\> 9

No. From the ri documentation of Integer#times:

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

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

This means that, in your case, it should call the blocks 9 times, from 0  
to 8.  
If it also passed 9 to the block, it would make 10 iterations, not 9.

If you want to include both 0 and 9, you can use upto instead of times:

0.upto(9){|x| puts “#{x} #{x.class}”}

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 14, 2007, 3:22am UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991/3 "2007-12-14T03:22:10Z")

</div>

From: Stefano C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# If you want to include both 0 and 9, you can use upto instead

# of times:

yes, and the fun part of ruby is that you can create your own and be  
creative as much as you like, too; a simple example

irb(main):025:0\> class Integer  
irb(main):026:1\> def mytimes start=0,inc=1  
irb(main):027:2\> x=start  
irb(main):028:2\> cnt=self  
irb(main):029:2\> while cnt \> 0  
irb(main):030:3\> yield x  
irb(main):031:3\> x+=inc  
irb(main):032:3\> cnt-=1  
irb(main):033:3\> end  
irb(main):034:2\> end  
irb(main):035:1\> end  
=\> nil  
irb(main):036:0\> 9.mytimes{|x| p x}  
0  
1  
2  
3  
4  
5  
6  
7  
8  
=\> nil  
irb(main):037:0\> 9.mytimes(1){|x| p x}  
1  
2  
3  
4  
5  
6  
7  
8  
9  
=\> nil  
irb(main):038:0\> 9.mytimes(2){|x| p x}  
2  
3  
4  
5  
6  
7  
8  
9  
10  
=\> nil  
irb(main):039:0\> 9.mytimes(2,2){|x| p x}  
2  
4  
6  
8  
10  
12  
14  
16  
18  
=\> nil  
irb(main):040:0\> 9.mytimes(2,-2){|x| p x}  
2  
0  
-2  
-4  
-6  
-8  
-10  
-12  
-14  
=\> nil  
irb(main):041:0\> 9.mytimes(2,0){|x| p x}  
2  
2  
2  
2  
2  
2  
2  
2  
2  
=\> nil  
irb(main):042:0\>

kind regards -botp

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 14, 2007, 3:56am UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991/4 "2007-12-14T03:56:43Z")

</div>

I cant resist, some refactor:

class Integer  
def mytimes(start=0, inc=1, &block)  
start.step(start + (self \* inc) - 1, inc, &block);  
end  
end

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 14, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991/5 "2007-12-14T14:04:07Z")

</div>

John M. wrote:

> Why does the output stop at eight. Shouldn’t the class of nine be  
> shown also?
> 
> 9.times {|x| print x , " ", x.class, “\n” }  
> 0 Fixnum  
> 1 Fixnum  
> 2 Fixnum  
> 3 Fixnum  
> 4 Fixnum  
> 5 Fixnum  
> 6 Fixnum  
> 7 Fixnum  
> 8 Fixnum  
> =\> 9

You might want another approach:

1.upto(9) {|x| print x , " ", x.class, “\n” }

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 14, 2007, 4:24am UTC](https://www.ruby-forum.com/t/simple-method-and-block-foo-times/122991/6 "2007-12-14T04:24:45Z")

</div>

From: Bernardo Monteiro R. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# class Integer

# def mytimes(start=0, inc=1, &block)

# start.step(start + (self \* inc) - 1, inc, &block);

ah, indeed; and as always, any refactoring/change must pass the test…

irb(main):011:0\> 0.mytimes(-1,-1){|x| p x}  
-1  
-2  
=\> -1  
irb(main):012:0\> 1.mytimes(-1,-1){|x| p x}  
-1  
-2  
-3  
=\> -1  
irb(main):013:0\> 2.mytimes(-1,-1){|x| p x}  
-1  
-2  
-3  
-4  
=\> -1  
irb(main):014:0\> 2.mytimes(-1,0){|x| p x}  
ArgumentError: step can’t be 0  
from (irb):3:in `step' from (irb):3:in `mytimes’  
from (irb):14  
from :0

btw, i do not like the way ruby arranges it’s params for #step. I prefer  
the step/inc arg to be the first arg since it reads clearly. eg,

instead of 1.step 10, 2  
i like 1.step 2, 10

reads clearly, and the step/inc is emphasize.

kind regards -botp
