New magical version of Symbol.to_proc

Trans wrote:

Charles O Nutter wrote:

The pluralization thing has too much potential for collision and looks
jarring (to_as looks to me like “to” “as”, which doesn’t make any
sense; then again I’m not a fan of programmatic pluralization for any
reason, since it’s very western-language-centric and far from
foolproof (moose? virus? fish?)).

You are right there. Pluralization is really going overboard, besides
the exceptions it creates a great deal of computational overhead. It
seems great on the surface, but in the end it is simply is not worth
the effort. Although it can seem odd for the English speaker at times,
Ruby’s general favoring of the singular is a very good thing. And this
is one area in which I feel Rails has been unhelpful.

The way I coded it, it singularizes the string and passes that as the
method call for the items of the collection. So if you pass a singular
method name, then it will still be singular when its passed to the
children.

That is:

people.names

returns the same list of names as

people.name

So non-English speakers can be happy too. Of course, if the people Array
class had a name method, the latter wouldn’t work. But unless you have
an Array of Arrays, you won’t get many name conflicts btw the Array
class and the container class.

Re: performance overhead - I’m not sure if the standard map+block is
more or less efficient than a for-loop, but I use it anyway because my
code is much more readable and writable. If I later discovered that I
had a performance bottleneck near my map+blocks, then I could refactor
them for speed.

I don’t think that in a thread on “fun syntax ideas” disapproving on the
ideas based on small performance hits like “apply a gsub on a string” is
much fun. :frowning:

Cheers
Nic

Seth Thomas R. wrote:

expressive power that seems to get closer to more intelligent programs,
p.s. I got totally lost with all that crazy.dot.talk. That hypothetic
code made me want to cry.

Rails :\

Seth Thomas R. wrote:

On the topic at hand, I don’t like the plurals option. The
Symbol#to_proc style currently in ActiveSupport seems just about
perfect to me, although I don’t see why it couldn’t just be:

foos.map :bar

It can…

class Array
def map(sym=nil, &block)
self.inject([]) { |col, item|
if block_given?
col << block.call(item)
elsif not sym.nil?
col << item.send(sym)
else
col << item
end
}
end
def map!(sym=nil, &block)
self.replace(map(sym, &block))
end
end

Regards,
Jordan

On 10/1/06, Dr Nic [email protected] wrote:

This would allow:
people.map :fullname, :split

as the equivalent of:
people.map {|p| p.fullname}.map {|n| n.split}

Oh so much fun :slight_smile:

I did this a while back by adding an f (for functional) to the method
name - so if you said people.mapf :fullname method_missing would see
that it ended with an f, strip the f out and construct a block to pass
to map. map_fullname reads even better, though.

martin

On 10/1/06, Dr Nic [email protected] wrote:

Re: performance overhead - I’m not sure if the standard map+block is
more or less efficient than a for-loop, but I use it anyway because my
code is much more readable and writable. If I later discovered that I
had a performance bottleneck near my map+blocks, then I could refactor
them for speed.

The JRuby code started out as a port of the C Ruby code, and for us a
‘for’ is just an ‘each’ over a range…so it’s probably about the
same, performance-wise.