# RCR Opinion Poll

**URL:** <https://www.ruby-forum.com/t/rcr-opinion-poll/82587>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587 "2007-01-19T16:31:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/1 "2007-01-19T16:31:31Z")

</div>

Hi all

there is one thing concerning enumerations I really would like to file a  
CR  
about. But I guess it is necessary to open my ears and listen to your  
POVs  
before doing so. I believe that similar things are already done in lots  
of  
extension libraries like e.g. facet.

The most concerning thing is that there will probably be no consensus  
about  
the magic dot notation, I know this has been discussed before.

Here we go

# Potential RCR, opinions wanted

The following is a very common pattern.

```
an_enum.map{ |ele| ele.a_method }

```

I believe that it is sufficiently common to be generalized.

There are two idioms I would like to see

an\_enum.apply\_to\_all :a\_method  
and  
an\_enum.apply\_to\_all.a\_method

the mutating version should also be available

an\_enum.apply\_to\_all! :a\_method  
and  
an\_enum.apply\_to\_all!.a\_method

Reference Implementation:

module Enumerable

```
def apply_to_all(method = nil)
    return ApplicationProxy.new( self, :modify => false ) unless 

```

method  
map { |ele| ele.send method }  
end #def apply\_to\_all(method = nil)

```
def apply_to_all!(method = nil)
    return ApplicationProxy.new( self, :modify => true ) unless 

```

method  
map! { |ele| ele.send method }  
end #def apply\_to\_all(method = nil)

```
class ApplicationProxy
    def initialize( enumerable, modifications = {} )
        @modify = modifications[:modify]
        @target = enumerable
    end
    def method_missing name, *args, &blk
        @modify ?
            @target.map!{ |ele| ele.send( name, *args, &blk ) } :
            @target.map{ |ele| ele.send( name, *args, &blk ) }
    end
end #class ApplicationProxy

```

# end #class Enumeration

Ty in advance

Robert

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 25, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/2 "2007-09-25T22:33:07Z")

</div>

On 1/12/07, Alexandru E. Ungur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > There are two idioms I would like to see
> > 
> > an\_enum.apply\_to\_all :a\_method
> 
> Ok, I’m still a newbie, but why not extend map instead of introducing a  
> new method? As in:

Sure a possibility, I personally like it - even with the magic dot  
notation  
(map.capitalize is also a possibility)  
if I recall correctly the map extension idea was rather badly seen the  
last  
time, so I did not think about it anymore.  
Thank you for the input.

```
%w[apples oranges kiwi].map :capitalize # => ["Apples", "Oranges",

```

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 25, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/3 "2007-09-25T22:34:14Z")

</div>

Hi –

On Fri, 12 Jan 2007, Robert D. wrote:

> Concerning the old RCRs that are not part of the new site (there are only  
> two RCR if I am not mistaken) is there any way to put them?  
> Can I help?

You can look at them for reference, but for 2.0 development Matz has  
asked that RCRs be resubmitted individually based on the new version  
of the site and the process.

David

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 25, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/4 "2007-09-25T22:39:32Z")

</div>

On 1/12/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> being more generalized. & invokes to\_proc on its operand; and to\_proc
> 
> –  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
> (See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

Matz, David

I knew about the &: kludge - I really hate it - but I did not know about  
the  
rejected RCR.  
That’s why I asked 😉  
ty for your time I will not file a RCR of course ☹

Concerning the old RCRs that are not part of the new site (there are  
only  
two RCR if I am not mistaken) is there any way to put them?  
Can I help?

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 25, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/5 "2007-09-25T22:46:46Z")

</div>

Hi,

In message “Re: RCR Opinion Poll”  
on Fri, 12 Jan 2007 18:56:08 +0900, “Robert D.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|The following is a very common pattern.  
|  
| an\_enum.map{ |ele| ele.a\_method }  
|  
|I believe that it is sufficiently common to be generalized.  
|  
|There are two idioms I would like to see  
|  
| an\_enum.apply\_to\_all :a\_method  
|and  
| an\_enum.apply\_to\_all.a\_method

an\_enum.map(&:a\_method) works for 1.9.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 25, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/6 "2007-09-25T22:41:05Z")

</div>

Hi –

On Fri, 12 Jan 2007, Alexandru E. Ungur wrote:

> > an\_enum.apply\_to\_all :a\_method
> 
> Ok, I’m still a newbie, but why not extend map instead of introducing a  
> new method? As in:
> 
> %w[apples oranges kiwi].map :capitalize # =\> [“Apples”, “Oranges”, “Kiwi”]
> 
> Still feels like the good old “map” and acts like it…

I know it’s sometimes hard to dig up the old RCRs (this one goes back  
two incarnations) but that was rejected; see  
[http://oldrcrs.rubypal.com/rejected.html#rcr50](http://oldrcrs.rubypal.com/rejected.html#rcr50).

The discussion for that RCR included:

- the suggestion of map(&:meth)
- using “apply” instead of “map” as the name
- rejection by Matz 🙂

The reason for rejection was: “It’s too functional so that I’m afraid  
it would not work well with OO nature of Ruby. Maybe it’s matter of  
notation.”

Ruby 1.9 has the map(&:meth) notation. I’m not its biggest fan  
(mostly on aesthetic/visual grounds), but it has the advantage of  
being more generalized. & invokes to\_proc on its operand; and to\_proc  
for symbols is defined as:

lambda {|e| e.send(self) }

There’s a lot you have to “just know” to grasp what map(&:meth) is  
doing, but basically it does this same thing of distributing a  
symbolically-identified method across an enumerable.

David

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [September 25, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/rcr-opinion-poll/82587/7 "2007-09-25T22:46:46Z")

</div>

> an\_enum.apply\_to\_all :a\_method

Ok, I’m still a newbie, but why not extend map instead of introducing a  
new method? As in:

```
%w[apples oranges kiwi].map :capitalize # => ["Apples", "Oranges", 

```

“Kiwi”]

Still feels like the good old “map” and acts like it…

Here’s my naive implementation, that produced the result above:

class Array  
alias :old\_map :map

def map(arg = nil, &block)  
if block  
old\_map &block  
elsif ! arg.nil?  
if arg.is\_a?(Symbol)  
old\_map {|x| x.send(arg)}  
else  
raise ArgumentError, “Don’t know how to handle #{arg.class}  
arguments”  
end  
else  
raise ArgumentError, “No parameter and no block given!”  
end  
end  
end

All the best,  
Alex
