Simple method and block. foo.times

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

Alle giovedì 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
=> 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

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/\.

# 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

···

From: Stefano Crocco [mailto:stefano.crocco@alice.it]

I cant resist, some refactor:

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

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

···

From: Bernardo Monteiro Rufino [mailto:bermonruf@gmail.com]
# 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