From: Jeff Mitchell Date: 2004-05-26T11:26:42+09:00 Subject: Re: [RCR] Numeric#of --- Harry Ohlsen wrote: > gabriele renzi wrote: > > > not if you don't want instances, say: > > > > them 3.instances_of {'foo'} > > Good point! > > I was thinking that the point of the method was to get unique objects, but of course it could be > used in this way. > > The "root cause" of this RCR (and others), I think, is the desire to create an Enumerable out of an Integer. Integers themselves are not Enumerable (nor should they be), but it is often desirable to think of them as such. Consider a = [] 3.times { a << "foo" } Integer#times is natural enough, but it is not an Enumeration -- and that is where #times does not match our needs. Thus we have require 'enumerator' 3.enum_for(:times).map { "foo" } or, the proposed abbreviation, 3.of { "foo" } It is clear there is a desire to convert from Integers to Enumerables freely. I think it is time to bring this into canon -- and I would like to call it Integer#enum. The proposed #of or #instances_of is thus a special case of Integer#enum, 3.enum.map { "foo" } which I happen to like the best. But the gem here is that it comes with all the power of Enumerable 6.enum.sort_by { rand } # => [4, 2, 5, 3, 1, 0] Now you may say, "well it's effectively the same as (0...n), who needs it?" First, I like 6.enum.foo better than (0...6).foo. Second, you cannot chain with Ranges, [1, 2, 3].inject(0) { |a, x| a+x }.enum.map { |x| x } would become (0...([1, 2, 3].inject(0) { |a, x| a+x })).map { |x| x } which I feel is awkward, if not confusing. __________________________________ Do you Yahoo!? Friends. Fun. Try the all-new Yahoo! Messenger. http://messenger.yahoo.com/