Numeric#of

A

Ara.T.Howard

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 :: http://www.ngdc.noaa.gov/stp/
| "640K ought to be enough for anybody." - Bill Gates, 1981
===============================================================================
 
A

Alex McHale

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
 
B

Bret Jolly

Ara.T.Howard said:
~ > 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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[RCR] Numeric#of 47
stable snapshot 4
parent of TrueClass, FalseClass 9
RMagick jp2 problem 8
Class::name 0
idiot's guide to druby using ssh tunnels 4
pretty exceptions 0
narray on windows? 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top