Numeric#of

i end up doing this sort of thing alot:

avg, max, min = {}, {}, {}

stack_a, stack_b = ,

and was thinking about a general syntax for ‘n of these’ or ‘n of the result
of this block’… does this read alright?

~ > cat of.rb
class Numeric
def of
ret =
times{|i| ret << yield(i)}
ret
end
end

them = 3.of{ Array.new }
p them # => [,,]

-a

···

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
“640K ought to be enough for anybody.” - Bill Gates, 1981
===============================================================================

I like it. I particularly like the added detail of yielding the index
into the creation, in case you need that info.

Kudos, and thanks for the snippet!

Alex McHale

···

On Tue, 25 May 2004 02:58:44 +0900, Ara.T.Howard ara.t.howard@noaa.gov wrote:

i end up doing this sort of thing alot:

avg, max, min = {}, {}, {}

stack_a, stack_b = ,

and was thinking about a general syntax for ‘n of these’ or ‘n of the result
of this block’… does this read alright?

~ > cat of.rb
class Numeric
def of
ret =
times{|i| ret << yield(i)}
ret
end
end

them = 3.of{ Array.new }
p them # => [,,]

-a

===============================================================================

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
“640K ought to be enough for anybody.” - Bill Gates, 1981
===============================================================================

“Ara.T.Howard” Ara.T.Howard@noaa.gov wrote in message news:Pine.LNX.4.44.0405241138150.20070-100000@fattire.ngdc.noaa.gov

~ > cat of.rb
class Numeric
def of
ret =
times{|i| ret << yield(i)}
ret
end
end

them = 3.of{ Array.new }
p them # => [,,]

irb(main):061:0> 6.of{1} + dozen.div(2).of{‘the other’}
[1, 1, 1, 1, 1, 1, “the other”, “the other”,
“the other”, “the other”, “the other”, “the other”]
Heh!