Velocity Reviews > Ruby > Simple method and block. foo.times

# Simple method and block. foo.times

John Maclean
Guest
Posts: n/a

 12-13-2007
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 Maclean
MSc (DIC)
+44 7739 171 531

Stefano Crocco
Guest
Posts: n/a

 12-13-2007
Alle gioved=EC 13 dicembre 2007, John Maclean ha scritto:
> 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
> =3D> 9

No. From the ri documentation of Integer#times:

int.times {|i| block } =3D> int
=2D-----------------------------------------------------------------------
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.=20
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

Peņa, Botp
Guest
Posts: n/a

 12-14-2007
From: Stefano Crocco [(E-Mail Removed)]=20
# If you want to include both 0 and 9, you can use upto instead=20
# 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=3D0,inc=3D1
irb(main):027:2> x=3Dstart
irb(main):028:2> cnt=3Dself
irb(main):029:2> while cnt > 0
irb(main):030:3> yield x
irb(main):031:3> x+=3Dinc
irb(main):032:3> cnt-=3D1
irb(main):033:3> end
irb(main):034:2> end
irb(main):035:1> end
=3D> nil
irb(main):036:0> 9.mytimes{|x| p x}
0
1
2
3
4
5
6
7
8
=3D> nil
irb(main):037:0> 9.mytimes(1){|x| p x}
1
2
3
4
5
6
7
8
9
=3D> nil
irb(main):038:0> 9.mytimes(2){|x| p x}
2
3
4
5
6
7
8
9
10
=3D> nil
irb(main):039:0> 9.mytimes(2,2){|x| p x}
2
4
6
8
10
12
14
16
18
=3D> nil
irb(main):040:0> 9.mytimes(2,-2){|x| p x}
2
0
-2
-4
-6
-8
-10
-12
-14
=3D> nil
irb(main):041:0> 9.mytimes(2,0){|x| p x}
2
2
2
2
2
2
2
2
2
=3D> nil
irb(main):042:0>

kind regards -botp

Bernardo Monteiro Rufino
Guest
Posts: n/a

 12-14-2007
[Note: parts of this message were removed to make it a legal post.]

I cant resist, some refactor:

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

Peņa, Botp
Guest
Posts: n/a

 12-14-2007
From: Bernardo Monteiro Rufino [(E-Mail Removed)]=20
# class Integer
# def mytimes(start=3D0, inc=3D1, &block)
# start.step(start + (self * inc) - 1, inc, &block);

ah, indeed; and as always, any refactoring/change must pass the test..=20

irb(main):011:0> 0.mytimes(-1,-1){|x| p x}
-1
-2
=3D> -1
irb(main):012:0> 1.mytimes(-1,-1){|x| p x}
-1
-2
-3
=3D> -1
irb(main):013:0> 2.mytimes(-1,-1){|x| p x}
-1
-2
-3
-4
=3D> -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,

i like 1.step 2, 10

reads clearly, and the step/inc is emphasize.

kind regards -botp

Guest
Posts: n/a

 12-14-2007
John Maclean 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" }
--
Posted via http://www.ruby-forum.com/.