[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

request for times, step, upto...

Peña, Botp

1/14/2005 7:06:00 AM

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




4 Answers

T. Onoma

1/14/2005 8:54:00 AM

0

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.



Robert Klemme

1/14/2005 9:47:00 AM

0


"Peña, Botp" <botp@delmonte-phil.com> schrieb im Newsbeitrag
news:20050114070636.2F2EB83F5@mx2.delmonte-phil.com...
> 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

Pit Capitain

1/14/2005 11:36:00 AM

0

Robert Klemme schrieb:

> "Peña, Botp" <botp@delmonte-phil.com> schrieb im Newsbeitrag
> news:20050114070636.2F2EB83F5@mx2.delmonte-phil.com...
>>
>>(...)
>>
>> 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.

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


Kristof Bastiaensen

1/14/2005 12:01:00 PM

0

On Fri, 14 Jan 2005 16:06:19 +0900, Peña, Botp wrote:
> <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