That’s quite nice. Are there any nasty side-effects? I suppose not,
since you must inform Ruby that you intend to use a symbol in place of
a proc with the ‘&’. I’ll chuck it in now, but it won’t see a release
for a while.
Thanks,
Gavin
···
On Sunday, October 5, 2003, 2:41:25 AM, Florian wrote:
Gavin Sinclair wrote:
Hi -talk,
Moin!
I have released Ruby (Standard Class) Extensions version 0.2.0.
Could you include this in the next release?
class Symbol
def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
end
It allows you do the following:
(1…10).inject(&:*) # => 3628800
%w{foo bar qux}.map(&:reverse) # => %w{oof rab xuq}
[1, 2, nil, 3, nil].reject(&:nil?) # => [1, 2, 3]
%w{ruby and world}.sort_by(&:reverse) # => %w{world and ruby}
class Symbol
def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
end
It allows you do the following:
(1…10).inject(&:*) # => 3628800
%w{foo bar qux}.map(&:reverse) # => %w{oof rab xuq}
[1, 2, nil, 3, nil].reject(&:nil?) # => [1, 2, 3]
%w{ruby and world}.sort_by(&:reverse) # => %w{world and ruby}
That’s quite nice. Are there any nasty side-effects? I suppose not,
since you must inform Ruby that you intend to use a symbol in place of
a proc with the ‘&’. I’ll chuck it in now, but it won’t see a release
for a while.
I like that, but I am already used to the “mapf” that someone proposed
long ago (“map function”).
It’s less general but it’s also less magic and takes one less
punctuation mark (though one more letter).
%w[foo bar bam].mapf(:reverse)
I’d vote for inclusion of that one also.
Hal
···
On Sunday, October 5, 2003, 2:41:25 AM, Florian wrote:
Yes, I like mapf; in fact I asked for it on this list many moons ago.
On the one hand, it’s an annoying specialisation of what
Symbol#to_proc allows. On the other hand, it’s funky. And #map is a
very commonly used method, and it’s used commonly with very simple
blocks, so it has special consideration.
It’s in the CVS.
Gavin
···
On Sunday, October 5, 2003, 11:39:15 AM, Hal wrote:
[…]
I like that, but I am already used to the “mapf” that someone proposed
long ago (“map function”).
It’s less general but it’s also less magic and takes one less
punctuation mark (though one more letter).