[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

stupid question

Joe Van Dyk

4/28/2005 9:17:00 PM

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



9 Answers

Dick Davies

4/28/2005 9:19:00 PM

0

* Joe Van Dyk <joevandyk@gmail.com> [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


Hal E. Fulton

4/28/2005 9:19:00 PM

0

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



Jason Yanowitz

4/28/2005 9:21:00 PM

0

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
>



James Gray

4/28/2005 9:28:00 PM

0

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



Joe Van Dyk

4/28/2005 10:50:00 PM

0

On 4/28/05, James Edward Gray II <james@grayproductions.net> 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?



Dick Davies

4/29/2005 1:33:00 PM

0

* Joe Van Dyk <joevandyk@gmail.com> [0450 23:50]:
> On 4/28/05, James Edward Gray II <james@grayproductions.net> 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


Joe Van Dyk

4/29/2005 3:31:00 PM

0

On 4/29/05, Kristof Bastiaensen <kristof@vleeuwen.org> wrote:
> On Fri, 29 Apr 2005 22:32:38 +0900, Dick Davies wrote:
>
> > * Joe Van Dyk <joevandyk@gmail.com> [0450 23:50]:
> >> On 4/28/05, James Edward Gray II <james@grayproductions.net> 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.



Kristof Bastiaensen

4/29/2005 3:32:00 PM

0

On Fri, 29 Apr 2005 22:32:38 +0900, Dick Davies wrote:

> * Joe Van Dyk <joevandyk@gmail.com> [0450 23:50]:
>> On 4/28/05, James Edward Gray II <james@grayproductions.net> 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

4/29/2005 4:25:00 PM

0

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 <kristof@vleeuwen.org> wrote:
> On Fri, 29 Apr 2005 22:32:38 +0900, Dick Davies wrote:
>
> > * Joe Van Dyk <joevandyk@gmail.com> [0450 23:50]:
> >> On 4/28/05, James Edward Gray II <james@grayproductions.net> 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?
>
>