Simple method and block. foo.times

Discussion in 'Ruby' started by John Maclean, Dec 13, 2007.

  1. John Maclean

    John Maclean Guest

    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
    John Maclean, Dec 13, 2007
    #1
    1. Advertising

  2. 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
    Stefano Crocco, Dec 13, 2007
    #2
    1. Advertising

  3. John Maclean

    Peña, Botp Guest

    From: Stefano Crocco [mailto:]=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
    Peña, Botp, Dec 14, 2007
    #3
  4. [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
    Bernardo Monteiro Rufino, Dec 14, 2007
    #4
  5. John Maclean

    Peña, Botp Guest

    From: Bernardo Monteiro Rufino [mailto:]=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
    Peña, Botp, Dec 14, 2007
    #5
  6. 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/.
    Lloyd Linklater, Dec 14, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    7
    Views:
    429
    Jerry Coffin
    Sep 21, 2003
  2. Replies:
    3
    Views:
    375
    John Roth
    Jul 29, 2005
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    382
    JaredGrubb
    Sep 24, 2008
  4. Max Williams
    Replies:
    10
    Views:
    197
    Max Williams
    Dec 15, 2007
  5. Replies:
    4
    Views:
    134
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page