Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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

 
Reply With Quote
 
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,

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

reads clearly, and the step/inc is emphasize.

kind regards -botp



 
Reply With Quote
 
Lloyd Linklater
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Centralizing global variables and functions - good & simple method? D. Shane Fowlkes ASP .Net 6 02-14-2006 03:11 PM
Is there a simple (and quick) serialisation method for simple vectors? Pierre Couderc C++ 1 07-29-2005 07:52 PM
What the diff between a virtual method and an inheirited method jlopes C++ 5 11-19-2004 09:43 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM



Advertisments