T
Trans
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
10.collect { |i| "No. #{i}" }
10.select { |i| i % 3 == 0 }
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:
10.times.collect { |i| "No. #{i}" }
10.times.select { |i| i % 3 == 0 }
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?
T.
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
10.collect { |i| "No. #{i}" }
10.select { |i| i % 3 == 0 }
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:
10.times.collect { |i| "No. #{i}" }
10.times.select { |i| i % 3 == 0 }
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?
T.