stupid question

J

Joe Van Dyk

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
 
D

Dick Davies

* Joe Van Dyk said:
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$
 
H

Hal Fulton

Joe said:
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
 
J

James Edward Gray II

rasputnik$ ri 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
 
J

Joe Van Dyk

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?
 
D

Dick Davies

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
 
J

Joe Van Dyk

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.
 
K

Kristof Bastiaensen

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?
 
K

Kent Sibilev

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top