From: dblack@... Date: 2006-07-21T22:22:53+09:00 Subject: Re: opacity of magic enumerator (was: Re: Using .each with constructors) Hi -- On Fri, 21 Jul 2006, Robert Dober wrote: > On 7/21/06, dblack@wobblini.net wrote: >> >> Maybe part of the problem is that it suggests an equivalence between: >> >> [0,1,2].map {} >> >> and >> >> 3.times.map {} > > > I completely agree, we could easily monkeypatch Fixnum to deliver > > 3.numbers => [0, 1, 2] > or > 3.numbers( :starting => 2 ) [2, 3, 4 ] > or > 3.numbers( :interval => 42 ) => [0, 42, 2*42 ] I'd rather not "monkeypatch" anything, but you could certainly extend Fixnum to do that, with the usual caveats about extending core classes. > I do not like the name, but maybe someone has a better idea? Your example made me think of upto, and indeed I find this much closer to being comprehensible than the 'times' version: >> 0.upto(3).map {|x| x * 10 } => [0, 10, 20, 30] It makes some semantic sense to think of "0.upto(3)" returning something that could be mapped, whereas "3.times" just doesn't sound right. Perhaps this all comes down to the need for more nuance and selectivity in deciding what each iterator returns, rather than having them all automatically return enumerators. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy http://www.manning.com/black => RUBY FOR RAILS (reviewed on Slashdot, 7/12/2006!) http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log dblack@wobblini.net => me