Smallest peeve ever!

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

I'm sure there's no chance of this ever changing but I'm disappointed
that the integer method "step":

start.step(end, step_size)

...wasn't more like:

step_size.step(start, end)

...I just like having the start and end points grouped together with the
step size singled out.
Who's with me?
 
P

Peña, Botp

From: Just Another Victim of the Ambient Morality=20
# start.step(end, step_size)
# ...wasn't more like:
# step_size.step(start, end)
# ...I just like having the start and end points grouped=20
# together with the=20
# step size singled out.
# Who's with me?=20

i'm w you. ruby is with you (quite).

irb(main):016:0> system "qri range.step"
------------------------------------------------------------- Range#step
rng.step(n=3D1) {| obj | block } =3D> 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.

irb(main):018:0* (0..10).step(2) {|x| p x}
0
2
4
6
8
10
=3D> 0..10

obviously, by definition of range, it will not cater negative steps=20

irb(main):030:0> (10..0).step(-2) {|x| p x}
ArgumentError: step can't be negative

:(

maybe, before i request a change in step, i might as well request a =
change in #succ/#next first, like succ receiving an optional argument, =
eg..

1.succ #=3D> 2
(-1).succ #=3D> 0

1.succ(1) #=3D> 2
(-1).succ(1 #=3D> 0

1.succ(2) #=3D> 3
(-1).succ(2) #=3D> 1

1.succ(-1) #=3D> 0
(-1).succ(-1)#=3D> -2

so then,

irb(main):030:0> (10..0).step(-2) {|x| p x}
10
8
6
4
2
0

I believe facets already does the succ(-n) part, but does not handle =
step(-n) which is pretty lame to me, imnho.

kind regards -botp
 
V

Vasyl Smirnov

I'm sure there's no chance of this ever changing but I'm disappointed
that the integer method "step":

start.step(end, step_size)

...wasn't more like:

step_size.step(start, end)

...I just like having the start and end points grouped together with the
step size singled out.
Who's with me?

You can help yourself :)

class Numeric
def mystep(from, to, &block)
raise ArgumentError unless block_given?
from.step(to, self) { |x| yield x }
end
end

5.step(10, 2) { |i| puts i }
2.mystep(5, 10) { |i| puts i }
 
V

Vasyl Smirnov

def mystep(from, to, &block)
third parameter is not necessary, just

def mystep(from, to)
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top