And so forth. This is nice, but, of course, the downside is that this
clutters up Integer with all these Enumerable methods.
Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning
an Enumerator. So most of these same functionality can be had just by
inserting #times in between:
This is great IMHO, so I thought I'd share. My only aside is the
thought that perhaps there's still a good reason to add Integer#to_a --
to save us the intermediate object of times.to_a. Or is that too much
of a YAGNI?
Thanks for the FYI. For what it's worth, I can't say I'm really a fan
of 10.times.select--it doesn't seem very clear to me what's being
iterated over. 10.select even less so. I think good ol'
(0...10).select looks better. *shrug*
Similarly, 10.to_a returning [0,1,...,9] doesn't really feel right to
me. In fact, I'd probably expect 10.to_a to do the same as Array(10)
(and changing the latter may well break things).
Just my thoughts.
Regards,
George.
···
On Dec 28, 2007 8:55 AM, Trans <transfire@gmail.com> wrote:
Just a little FYI.
Not too long ago I noted this suggestion:
class Integer
alias_method :each, :times
include Enumerable
end
Which is kind of cool in that allows some convenient statements like
And so forth. This is nice, but, of course, the downside is that this
clutters up Integer with all these Enumerable methods.
Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning
an Enumerator. So most of these same functionality can be had just by
inserting #times in between:
This is great IMHO, so I thought I'd share. My only aside is the
thought that perhaps there's still a good reason to add Integer#to_a --
to save us the intermediate object of times.to_a. Or is that too much
of a YAGNI?
Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning
an Enumerator. So most of these same functionality can be had just by
inserting #times in between:
This is great IMHO, so I thought I'd share. My only aside is the
thought that perhaps there's still a good reason to add Integer#to_a --
to save us the intermediate object of times.to_a. Or is that too much
of a YAGNI?
I'd rather use 10.times.to_a instead of 10.to_a because the latter
seems too unobvious to me.
Kind regards
robert
···
2007/12/27, Trans <transfire@gmail.com>:
--
use.inject do |as, often| as.you_can - without end
Good point. I'd like to point out that the overhead completely depends on the number where #to_a is invoked on. For small numbers the overhead is likely significant. But for large numbers, especially BigNums I'd say the overhead is negligible. But I guess, in reality this would be more often used with rather smallish numbers. So yes, this should be taken into account.
Kind regards
robert
···
On 28.12.2007 19:30, Trans wrote:
On Dec 28, 12:01 pm, "Robert Klemme" <shortcut...@googlemail.com> > wrote:
I'd rather use 10.times.to_a instead of 10.to_a because the latter
seems too unobvious to me.
Understandable. But obvious isn't always the most important factor. A
straight #to_a is vastly more efficient.