[RCR] Numeric#of

M

Martin DeMello

Kristof Bastiaensen said:
I think it will not work with this syntax.
What means the following:
mymethod [ a ]
call mymethod, and collect a for each iteration, or call mymethod
with an array?

Same peoblem with hashes and blocks now, no?
to_enum.to_a works with the current syntax:
(2..10).to_enum:)step, 3).to_a
=> [2, 5, 8]

Maybe a to_enum! (or #map_enum, or #map_over or #gather ...) that works
like to_enum but does the .to_a step at the end for you. (I really wish
to_enum had been called simply enum, incidentally - I'm a big fan of
brevity. Not just typing - as Paul Graham pointed out, to_enum takes
longer to say to yourself when reading over code, and thus slows you
down.)
That's true. I think every common symbol is used.
Fortunately they are arranged so as not to make ugly combinations.
Imagine having to type $_->$*@ or something.
Adding new syntax in an elegant way is not so easy...

Very true. And Ruby has an excellent track record in that respect.

martin
 
M

Michael Campbell

I think it will not work with this syntax.
What means the following:
mymethod [ a ]
call mymethod, and collect a for each iteration, or call mymethod
with an array?

Get rid of "poetry mode" and there's no ambiguity.

mymethod[a] # collect a for each iteration

mymethod([a]) # call with an array


(Yes, I know I'm tilting at windmills here, the suggestion is
rhetorical, call off any poetry dogs, please.)
 
G

gabriele renzi

il Tue, 25 May 2004 15:37:04 -0600, "Ara.T.Howard" <[email protected]>
ha scritto::

how about this?


Object#collect


them = collect(3){ Array.new }

should'nt this be Kernel::collect?
Anyway, I'd prefer to see Object#* :)
them = Array.new * 3 # actually dup/clone
 
A

Alan Chen

gabriele renzi said:
il Tue, 25 May 2004 15:37:04 -0600, "Ara.T.Howard" <[email protected]>
ha scritto::



should'nt this be Kernel::collect?
Anyway, I'd prefer to see Object#* :)
them = Array.new * 3 # actually dup/clone

I had one suggestion which I think was lost in the recent gateway
debug process... I'll repeat it again below - hopefully it wasn't a
simply bad idea :)

To me, creating multiple instances of initialized objects feels like
it belongs more with the new call, rather than Integer. Perhaps we
could add a "multiple instance" new? Here is one implementation.

def Object.newx( nx, *args)
instances = []
if( block_given? )
nx.times { instances.push yield(self.new(*args)) }
else
nx.times { instances.push self.new(*args) }
end
instances
end


a,b,c = Array.newx(3)

I'm not sure what should be done when the new also expects a block.
 
A

Ara.T.Howard

gabriele renzi said:
il Tue, 25 May 2004 15:37:04 -0600, "Ara.T.Howard" <[email protected]>
ha scritto::



should'nt this be Kernel::collect?
Anyway, I'd prefer to see Object#* :)
them = Array.new * 3 # actually dup/clone

I had one suggestion which I think was lost in the recent gateway
debug process... I'll repeat it again below - hopefully it wasn't a
simply bad idea :)

To me, creating multiple instances of initialized objects feels like
it belongs more with the new call, rather than Integer. Perhaps we
could add a "multiple instance" new? Here is one implementation.

def Object.newx( nx, *args)
instances = []
if( block_given? )
nx.times { instances.push yield(self.new(*args)) }
else
nx.times { instances.push self.new(*args) }
end
instances
end


a,b,c = Array.newx(3)

I'm not sure what should be done when the new also expects a block.

i see your point, but my original idea was not only for constructing objects,
but for doing 'something' n times:

admittedly lame example:

results = 16.of{|n| db.execute(build_query(n)) }

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it; and a weed grows, even though we do
| not love it. --Dogen
===============================================================================
 
K

Kristof Bastiaensen

Hi,

To me, creating multiple instances of initialized objects feels like
it belongs more with the new call, rather than Integer.

Yes, in fact it already works with the new call ;-)
a,b,c = Array.newx(3)

a,b,c = Array.new(3) { Array.new }

Regards,
Kristof
 
D

David A. Black

Hi --

il Tue, 25 May 2004 15:37:04 -0600, "Ara.T.Howard" <[email protected]>
ha scritto::

I don't think "collect" is suitable as all-purpose/top-level method.
It really means: collect results from filtering an enumerable object's
elements. You could say the object here is 3, but that's got an
awfully procedural and kind of backwards feel to it (assigning the
role of the receiver to an argument).
should'nt this be Kernel::collect?
Anyway, I'd prefer to see Object#* :)
them = Array.new * 3 # actually dup/clone

I think * is already claimed by too many existing classes to be
reclaimed for this (for example, "abc" * 3, or 10 * 3).


David
 
A

Alan Chen

Kristof Bastiaensen said:
Hi,



Yes, in fact it already works with the new call ;-)


a,b,c = Array.new(3) { Array.new }

Regards,
Kristof

Yes, but can you do it for any object?

class Dbrec
attr_accessor :city, :state
def initialize
@name = ''
@state = ''
end
end

losangeles,sandiego = Dbrec.newx(2)

or

losangeles,sandiego = Dbrec.newx(2) { |rec| rec.state = 'CA'; rec }

If anyone was actually serious about a newx method, you might want to
eliminate the need for the last "; rec" in the block above. Now, Ara
was looking for a general "do it x times and collect the results"
method and this is a "instantiate multiple mostly, similar objects"
method. My hunch is that neither one is necessary, and would mostly
trade a minor convience for a minor obfuscation of code.
 

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

Numeric#of 2
stable snapshot 4
Object#copy [rcr?] 4
[RCR] Kernel#hostname 0
parent of TrueClass, FalseClass 9
RMagick jp2 problem 8
RCR - 'struct flock*' wrapper for rb_io_fcntl 7
narray on windows? 1

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top