request for times, step, upto...

P

Peña, Botp

Hi,

1. I am a Integer#times fan :) So you see my (and my kids) nuby codes
littered w it. I have a small request though.
Can we pass initial and step params?

ie

from int.times {|i| block }

to int.times(starting=0, step=1) {|i| block }

I hope this would not break old code, right?


2. my request may be handled by step. But I do not like step (me only);
maybe because the sound does not ring or maybe because I do not like the
order of params when read.

iow

from num.step(limit, step ) {|i| block },

I prefer fr_num.step(step, upto ) {|i| block }

since I would read the ff 1.step(2,5) {|x| p x} as
"from 1 step 2 up to 5" (I know this would break old code)

I feel Integer#upto has a better read -"int.upto(limit) {|i| block }", but
sadly #upto has no steps either :(

thanks for reading =)

kind regards -botp
 
T

trans. (T. Onoma)

On Friday 14 January 2005 02:06 am, "Peña, Botp" wrote:
| Hi,
|
| 1. I am a Integer#times fan :) So you see my (and my kids) nuby codes
| littered w it. I have a small request though.
| Can we pass initial and step params?
|
| ie
|
| from int.times {|i| block }
|
| to int.times(starting=0, step=1) {|i| block }
|
| I hope this would not break old code, right?
|
|
| 2. my request may be handled by step. But I do not like step (me only);
| maybe because the sound does not ring or maybe because I do not like the
| order of params when read.
|
| iow
|
| from num.step(limit, step ) {|i| block },
|
| I prefer fr_num.step(step, upto ) {|i| block }
|
| since I would read the ff 1.step(2,5) {|x| p x} as
| "from 1 step 2 up to 5" (I know this would break old code)
|
| I feel Integer#upto has a better read -"int.upto(limit) {|i| block }", but
| sadly #upto has no steps either :(
|
| thanks for reading =)
|
| kind regards -botp

Whether these make it into Ruby core or not (I'm all for it) I will definitely
add these mods too Ruby Facets (unless of course someone can show that its a
really bad idea, but I doubt that). Do you have them coded up already by
chance?

Thanks,
T.
 
R

Robert Klemme

Peña said:
Hi,

1. I am a Integer#times fan :) So you see my (and my kids) nuby codes
littered w it. I have a small request though.
Can we pass initial and step params?

ie

from int.times {|i| block }

to int.times(starting=0, step=1) {|i| block }

I hope this would not break old code, right?

Sure, but semantics of this method will be broken. The block will no
longer execute int times. We have #step for that as know.
2. my request may be handled by step. But I do not like step (me only);
maybe because the sound does not ring or maybe because I do not like the
order of params when read.

Ah, so you *do* care about the correlation between semantics and method
names.
iow

from num.step(limit, step ) {|i| block },

I prefer fr_num.step(step, upto ) {|i| block }

since I would read the ff 1.step(2,5) {|x| p x} as
"from 1 step 2 up to 5" (I know this would break old code)

I feel Integer#upto has a better read -"int.upto(limit) {|i| block }", but
sadly #upto has no steps either :(

That could be added. And it would be a resonable and good change IMHO.
Same for #downto btw.
thanks for reading =)

Thanks for calling. :)

Kind regards

robert
 
P

Pit Capitain

Robert said:
Sure, but semantics of this method will be broken. The block will no
longer execute int times. We have #step for that as know.

I thought he just wanted to change the values passed into the block. Instead of

0, 1, ..., n-1

it would be

start, start + step, ..., start + (n-1)*step

Looks useful to me.

Regards,
Pit
 
K

Kristof Bastiaensen

<snip>
2. my request may be handled by step. But I do not like step (me only);
maybe because the sound does not ring or maybe because I do not like the
order of params when read.

Hi,

I agree that Integer#step looks a bit odd. The most
natural way to do this is IMO with Range#step (added
in 1.8? ):

irb(main):001:0> (2..9).step(2) do |i| puts i end
2
4
6
8

KB
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top