stupid question

Discussion in 'Ruby' started by Joe Van Dyk, Apr 28, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    How can I neatly iterate from 0 to some number (either pos or neg)
    while skipping over some number of numbers?

    How's that for confusing?

    So,

    If the ending number is 550 and I want to skip by 100s, it would print
    0, 100, 200, 300, 400, 500.

    If the ending number is -552 and I want to skip by 100s, it would print
    0, -100, -200, -300, -400, -500

    There's nothing in the standard library that does this kind of thing, is there?

    Thanks,
    Joe
    Joe Van Dyk, Apr 28, 2005
    #1
    1. Advertising

  2. Joe Van Dyk

    Dick Davies Guest

    * Joe Van Dyk <> [0417 22:17]:
    > How can I neatly iterate from 0 to some number (either pos or neg)
    > while skipping over some number of numbers?
    >
    > How's that for confusing?
    >
    > So,
    >
    > If the ending number is 550 and I want to skip by 100s, it would print
    > 0, 100, 200, 300, 400, 500.
    >
    > If the ending number is -552 and I want to skip by 100s, it would print
    > 0, -100, -200, -300, -400, -500
    >
    > There's nothing in the standard library that does this kind of thing, is there?


    rasputnik$ ri Numeric#step
    ----------------------------------------------------------- Numeric#step
    num.step(limit, step ) {|i| block } => num
    ------------------------------------------------------------------------
    Invokes block with the sequence of numbers starting at num,
    incremented by step on each call. The loop finishes when the value
    to be passed to the block is greater than limit (if step is
    positive) or less than limit (if step is negative). If all the
    arguments are integers, the loop operates using an integer
    counter. If any of the arguments are floating point numbers, all
    are converted to floats, and the loop is executed floor(n +
    n*epsilon)+ 1 times, where n = (limit - num)/step. Otherwise, the
    loop starts at num, uses either the < or > operator to compare the
    counter against limit, and increments itself using the + operator.

    1.step(10, 2) { |i| print i, " " }
    Math::E.step(Math::pI, 0.2) { |f| print f, " " }

    produces:

    1 3 5 7 9
    2.71828182845905 2.91828182845905 3.11828182845905

    rasputnik$

    --
    'Tempers are wearing thin. Let's hope some robot doesn't kill everybody.'
    -- Bender
    Rasputin :: Jack of All Trades - Master of Nuns
    Dick Davies, Apr 28, 2005
    #2
    1. Advertising

  3. Joe Van Dyk

    Hal Fulton Guest

    Joe Van Dyk wrote:
    > How can I neatly iterate from 0 to some number (either pos or neg)
    > while skipping over some number of numbers?
    >
    > How's that for confusing?
    >
    > So,
    >
    > If the ending number is 550 and I want to skip by 100s, it would print
    > 0, 100, 200, 300, 400, 500.
    >
    > If the ending number is -552 and I want to skip by 100s, it would print
    > 0, -100, -200, -300, -400, -500
    >
    > There's nothing in the standard library that does this kind of thing, is there?


    I think there's a Fixnum#step or something.


    Hal
    Hal Fulton, Apr 28, 2005
    #3
  4. 0.step(550,100) { |i| puts i }
    0.step(-552,-100) { |i| puts i }

    On Apr 28, 2005, at 5:17 PM, Joe Van Dyk wrote:

    > How can I neatly iterate from 0 to some number (either pos or neg)
    > while skipping over some number of numbers?
    >
    > How's that for confusing?
    >
    > So,
    >
    > If the ending number is 550 and I want to skip by 100s, it would print
    > 0, 100, 200, 300, 400, 500.
    >
    > If the ending number is -552 and I want to skip by 100s, it would print
    > 0, -100, -200, -300, -400, -500
    >
    > There's nothing in the standard library that does this kind of thing,
    > is there?
    >
    > Thanks,
    > Joe
    >
    Jason Yanowitz, Apr 28, 2005
    #4
  5. On Apr 28, 2005, at 4:18 PM, Dick Davies wrote:

    > rasputnik$ ri Numeric#step
    > -----------------------------------------------------------
    > Numeric#step


    I the new version which I think is cool:

    ------------------------------------------------------------- Range#step
    rng.step(n=1) {| obj | block } => rng
    ------------------------------------------------------------------------
    Iterates over rng, passing each nth element to the block. If the
    range contains numbers or strings, natural ordering is used.
    Otherwise step invokes succ to iterate through range elements. The
    following code uses class Xs, which is defined in the class-level
    documentation.

    range = Xs.new(1)..Xs.new(10)
    range.step(2) {|x| puts x}
    range.step(3) {|x| puts x}

    produces:

    1 x
    3 xxx
    5 xxxxx
    7 xxxxxxx
    9 xxxxxxxxx
    1 x
    4 xxxx
    7 xxxxxxx

    Hope that helps.

    James Edward Gray II
    James Edward Gray II, Apr 28, 2005
    #5
  6. Joe Van Dyk

    Joe Van Dyk Guest

    On 4/28/05, James Edward Gray II <> wrote:
    > On Apr 28, 2005, at 4:18 PM, Dick Davies wrote:
    >
    > > rasputnik$ ri Numeric#step
    > > -----------------------------------------------------------
    > > Numeric#step

    >
    > I the new version which I think is cool:
    >
    > ------------------------------------------------------------- Range#step
    > rng.step(n=1) {| obj | block } => rng
    > ------------------------------------------------------------------------
    > Iterates over rng, passing each nth element to the block. If the
    > range contains numbers or strings, natural ordering is used.
    > Otherwise step invokes succ to iterate through range elements. The
    > following code uses class Xs, which is defined in the class-level
    > documentation.
    >
    > range = Xs.new(1)..Xs.new(10)
    > range.step(2) {|x| puts x}
    > range.step(3) {|x| puts x}
    >
    > produces:
    >
    > 1 x
    > 3 xxx
    > 5 xxxxx
    > 7 xxxxxxx
    > 9 xxxxxxxxx
    > 1 x
    > 4 xxxx
    > 7 xxxxxxx
    >


    But that only goes up, right?
    Joe Van Dyk, Apr 28, 2005
    #6
  7. Joe Van Dyk

    Dick Davies Guest

    * Joe Van Dyk <> [0450 23:50]:
    > On 4/28/05, James Edward Gray II <> wrote:
    > > ------------------------------------------------------------- Range#step
    > > rng.step(n=1) {| obj | block } => rng
    > > ------------------------------------------------------------------------


    > But that only goes up, right?


    Apparently so -

    irb(main):004:0> (100..10).step(-2) { |x| puts x }
    ArgumentError: step can't be negative
    from (irb):4:in `step'
    from (irb):4


    --
    'Everybody's a jerk. You, me, this jerk.'
    -- Bender
    Rasputin :: Jack of All Trades - Master of Nuns
    Dick Davies, Apr 29, 2005
    #7
  8. Joe Van Dyk

    Joe Van Dyk Guest

    On 4/29/05, Kristof Bastiaensen <> wrote:
    > On Fri, 29 Apr 2005 22:32:38 +0900, Dick Davies wrote:
    >
    > > * Joe Van Dyk <> [0450 23:50]:
    > >> On 4/28/05, James Edward Gray II <> wrote:
    > >> > ------------------------------------------------------------- Range#step
    > >> > rng.step(n=1) {| obj | block } => rng
    > >> > ------------------------------------------------------------------------

    > >
    > >> But that only goes up, right?

    > >
    > > Apparently so -
    > >
    > > irb(main):004:0> (100..10).step(-2) { |x| puts x }
    > > ArgumentError: step can't be negative
    > > from (irb):4:in `step'
    > > from (irb):4

    >
    > Worthy of a RCR?


    I dunno. In my case, I have to check to see if what I want to iterate
    to is greater than or less than zero. Then I change the 'step'
    increment/decrement value to be either positive or negative,
    respectively. It's not that difficult but it's sorta clunky.
    Joe Van Dyk, Apr 29, 2005
    #8
  9. On Fri, 29 Apr 2005 22:32:38 +0900, Dick Davies wrote:

    > * Joe Van Dyk <> [0450 23:50]:
    >> On 4/28/05, James Edward Gray II <> wrote:
    >> > ------------------------------------------------------------- Range#step
    >> > rng.step(n=1) {| obj | block } => rng
    >> > ------------------------------------------------------------------------

    >
    >> But that only goes up, right?

    >
    > Apparently so -
    >
    > irb(main):004:0> (100..10).step(-2) { |x| puts x }
    > ArgumentError: step can't be negative
    > from (irb):4:in `step'
    > from (irb):4


    Worthy of a RCR?
    Kristof Bastiaensen, Apr 29, 2005
    #9
  10. Joe Van Dyk

    Kent Sibilev Guest

    But you still can do:

    irb(main):001:0> 100.step(0, -10){|i| puts i}
    100
    90
    80
    70
    60
    50
    40
    30
    20
    10
    0
    => 100
    irb(main):002:0>

    On 4/29/05, Kristof Bastiaensen <> wrote:
    > On Fri, 29 Apr 2005 22:32:38 +0900, Dick Davies wrote:
    >
    > > * Joe Van Dyk <> [0450 23:50]:
    > >> On 4/28/05, James Edward Gray II <> wrote:
    > >> > ------------------------------------------------------------- Range#step
    > >> > rng.step(n=1) {| obj | block } => rng
    > >> > ------------------------------------------------------------------------

    > >
    > >> But that only goes up, right?

    > >
    > > Apparently so -
    > >
    > > irb(main):004:0> (100..10).step(-2) { |x| puts x }
    > > ArgumentError: step can't be negative
    > > from (irb):4:in `step'
    > > from (irb):4

    >
    > Worthy of a RCR?
    >
    >
    Kent Sibilev, Apr 29, 2005
    #10
    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. Michael W Cocke

    Stupid RE question

    Michael W Cocke, Dec 27, 2004, in forum: Perl
    Replies:
    2
    Views:
    422
    Michael W Cocke
    Dec 28, 2004
  2. Steve

    Is this a stupid question?

    Steve, Jul 17, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    340
    Mario Vargas
    Jul 17, 2003
  3. Will

    Stupid C# question Please help!

    Will, Aug 8, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    441
    carlos medina
    Aug 9, 2003
  4. Brandon McCombs
    Replies:
    4
    Views:
    503
    Richard Wheeldon
    Aug 28, 2006
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,011
Loading...

Share This Page