# Cleaner syntax for .map (is there already a way, or ruby2 idea?)

**URL:** <https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938>\
**Category:** ruby-talk\
**Created:** [27 October 2005 06:05 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938 "2005-10-27T06:05:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_M](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@Ron\_M](https://rubytalk.org/u/Ron_M)\
**Post date:** [27 October 2005 06:05 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/1 "2005-10-27T06:05:41Z")

</div>

I find that a bunch of my code looks like

&nbsp;&nbsp;&nbsp;people.map{|person| person.email\_addr}

where I use map to apply a single method to elements inside arrays.

That sure beats C, but wouldn't it be nice if there  
were a shorthand for doing so. I think something  
like this would be very nice:

&nbsp;&nbsp;&nbsp;people[\*].email\_addr

Even better, I often have strings of such stuff that  
looks like this, creating nested arrays

&nbsp;&nbsp;&nbsp;departments.map{|dept| dept.people.map{|person| person.email\_addr}}

where a much cleaner alternative would be

&nbsp;&nbsp;&nbsp;departments[\*].people[\*].email\_addr

so the "[\*]" operator would descend into nested arrays.

In total ignorance of whether the parser would  
allow it, I'd like to say that I think that'd  
be a nice addition to ruby2 if there's no other  
clean shorthand out there already.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [27 October 2005 06:25 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/2 "2005-10-27T06:25:12Z")

</div>

Ron M wrote:

> I find that a bunch of my code looks like
> 
> &nbsp;&nbsp;people.map{|person| person.email\_addr}
> 
> where I use map to apply a single method to elements inside arrays.
> 
> That sure beats C, but wouldn't it be nice if there  
> were a shorthand for doing so. I think something  
> like this would be very nice:
> 
> &nbsp;&nbsp;people[\*].email\_addr

google for "symbol to\_proc". For example:

[http://extensions.rubyforge.org/rdoc/classes/Symbol.html](http://extensions.rubyforge.org/rdoc/classes/Symbol.html)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [27 October 2005 06:37 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/3 "2005-10-27T06:37:05Z")

</div>

Ron M:

> I find that a bunch of my code looks like
> 
> &nbsp;&nbsp;people.map{|person| person.email\_addr}
> 
> where I use map to apply a single method to elements inside arrays.

I've done something like this before:

module Enumerable  
&nbsp;&nbsp;def map\_send(\*methods)  
&nbsp;&nbsp;&nbsp;&nbsp;map {|obj| methods.inject(obj) {|obj, meth| obj.send(meth) }}  
&nbsp;&nbsp;end  
end  
a = %w{ foo bar baz}  
a.map\_send(:capitalize, :reverse) #=\> ["ooF", "raB", "zaB"]

> .... I think something  
> like this would be very nice:
> 
> &nbsp;&nbsp;people[\*].email\_addr

That's an interesting-looking operator there. I think it's very unclear what  
it does. I'd prefer the mouse-poo version  
&nbsp;&nbsp;people.map { @.email\_addr }  
or  
&nbsp;&nbsp;people.map { it.email\_addr }

I don't think Enumerable wants a special operator like this; method names  
convey meaning.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [27 October 2005 08:46 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/4 "2005-10-27T08:46:17Z")

</div>

Hi,

At Thu, 27 Oct 2005 15:05:41 +0900,  
Ron M wrote in [ruby-talk:162876]:

> That sure beats C, but wouldn't it be nice if there  
> were a shorthand for doing so. I think something  
> like this would be very nice:
> 
> &nbsp;&nbsp;&nbsp;people[\*].email\_addr

What about:

&nbsp;&nbsp;module Mappable  
&nbsp;&nbsp;&nbsp;&nbsp;class Mapper  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(obj)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.map {|i| i.send(meth, \*args, &block)}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;def self.included(klass)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aref = klass.instance\_method(:)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:) do |\*idx|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if idx.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mapper.new(self)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aref[\*idx]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;class Array  
&nbsp;&nbsp;&nbsp;&nbsp;include Mappable  
&nbsp;&nbsp;end

&nbsp;&nbsp;a = %w[a b c]  
&nbsp;&nbsp;p a.upcase

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [27 October 2005 12:07 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/5 "2005-10-27T12:07:03Z")

</div>

require 'facets/enumerable/op\_mod'

&nbsp;&nbsp;people.%.email\_addr

A shorthand for:

&nbsp;&nbsp;people.every.email\_addr

But I like your [\*]. Hmm... I maybe able to adjust above to use [:\*]  
--I'll see.

T.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Joyeux](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Sylvain\_Joyeux](https://rubytalk.org/u/Sylvain_Joyeux)\
**Post date:** [27 October 2005 06:47 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/6 "2005-10-27T06:47:11Z")

</div>

> google for "symbol to\_proc". For example:  
> [http://extensions.rubyforge.org/rdoc/classes/Symbol.html](http://extensions.rubyforge.org/rdoc/classes/Symbol.html)

Very nice this one !

> **···**
>
> --  
> Sylvain Joyeux

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 October 2005 09:07 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/7 "2005-10-27T09:07:14Z")

</div>

nobuyoshi nakada wrote:

> Hi,
> 
> At Thu, 27 Oct 2005 15:05:41 +0900,  
> Ron M wrote in [ruby-talk:162876]:
> 
> > That sure beats C, but wouldn't it be nice if there  
> > were a shorthand for doing so. I think something  
> > like this would be very nice:
> > 
> > &nbsp;&nbsp;&nbsp;people[\*].email\_addr
> 
> What about:
> 
> &nbsp;&nbsp;module Mappable  
> &nbsp;&nbsp;&nbsp;&nbsp;class Mapper  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(obj)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.map {|i| i.send(meth, \*args, &block)}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;def self.included(klass)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aref = klass.instance\_method(:)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:) do |\*idx|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if idx.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mapper.new(self)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aref[\*idx]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;class Array  
> &nbsp;&nbsp;&nbsp;&nbsp;include Mappable  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;a = %w[a b c]  
> &nbsp;&nbsp;p a.upcase

I don't like it because now does two completely different things:  
access and mapping. I'd prefer something like this:

module Enumerable  
&nbsp;&nbsp;# replacement for map  
&nbsp;&nbsp;def mapx(\*a,&b)  
&nbsp;&nbsp;&nbsp;&nbsp;raise "Can only have one" if !a.empty? && b  
&nbsp;&nbsp;&nbsp;&nbsp;if a.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map(&b)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map {|x| a.inject(x) {|v,m| v.send(m)}}  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

> > %w{a b c}.mapx :upcase

=\> ["A", "B", "C"]

> > %w{a b c}.mapx {|a| a + "x"}

=\> ["ax", "bx", "cx"]

> > %w{abc bcd cde}.mapx :upcase, :reverse

=\> ["CBA", "DCB", "EDC"]

I just chose mapx to get it working fast, ideally the original map method  
of Enumerable classes would be changed.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [27 October 2005 10:32 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/8 "2005-10-27T10:32:24Z")

</div>

As always, there's something magical about your code! Unfortunately,  
it makes pp blow up for one 😉

For a similar purpose, I use this - not as slick but simple and  
understandable. Doesn't handle the nesting though.

module Enumerable  
&nbsp;&nbsp;def where(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;self.select{|x| x.instance\_eval(&block) }  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def project(&block)  
&nbsp;&nbsp;&nbsp;&nbsp;self.map{|x| x.instance\_eval(&block) }  
&nbsp;&nbsp;end  
end

as in

&nbsp;&nbsp;depts.where{ name == "Apps" }.project{ people.project{ name } }

Regards,

Sean

> **···**
>
> On 10/27/05, nobuyoshi nakada \<nobuyoshi.nakada@ge.com\> wrote:
> 
> > What about:
> > 
> > module Mappable  
> > &nbsp;&nbsp;&nbsp;class Mapper  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize(obj)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@obj.map {|i| i.send(meth, \*args, &block)}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;def self.included(klass)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aref = klass.instance\_method(:)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:) do |\*idx|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if idx.empty?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mapper.new(self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aref[\*idx]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > class Array  
> > &nbsp;&nbsp;&nbsp;include Mappable  
> > end
> > 
> > a = %w[a b c]  
> > p a.upcase
> > 
> > --  
> > Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Nakada\_Nobuyoshi](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nakada\_Nobuyoshi](https://rubytalk.org/u/Nakada_Nobuyoshi)\
**Post date:** [28 October 2005 02:45 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/9 "2005-10-28T02:45:11Z")

</div>

Hi,

At Thu, 27 Oct 2005 21:07:03 +0900,  
Trans wrote in [ruby-talk:162907]:

> A shorthand for:
> 
> &nbsp;&nbsp;people.every.email\_addr
> 
> But I like your [\*]. Hmm... I maybe able to adjust above to use [:\*]

I feel your "every" much cleaner than [\*].

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [27 October 2005 10:52 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/10 "2005-10-27T10:52:03Z")

</div>

That is neat - I never thought of unifying map and mapx

martin

> **···**
>
> Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > \>\> %w{a b c}.mapx :upcase  
> > =\> ["A", "B", "C"]  
> > \>\> %w{a b c}.mapx {|a| a + "x"}  
> > =\> ["ax", "bx", "cx"]  
> > \>\> %w{abc bcd cde}.mapx :upcase, :reverse  
> > =\> ["CBA", "DCB", "EDC"]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [28 October 2005 05:11 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/11 "2005-10-28T05:11:06Z")

</div>

Hi --

> **···**
>
> On Fri, 28 Oct 2005, nobuyoshi nakada wrote:
> 
> > Hi,
> > 
> > At Thu, 27 Oct 2005 21:07:03 +0900,  
> > Trans wrote in [ruby-talk:162907]:
> > 
> > > A shorthand for:
> > > 
> > > &nbsp;&nbsp;people.every.email\_addr
> > > 
> > > But I like your [\*]. Hmm... I maybe able to adjust above to use [:\*]
> > 
> > I feel your "every" much cleaner than [\*].
> 
> I agree, visually, but I find both of the semantically opaque compared  
> to people.each {|person| ... } I know that people.every could return  
> some kind of generator or enumerator, which could then be fed  
> "email\_addr" symbolically... but it seems to conceal rather than  
> reveal what's going on.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [28 October 2005 05:45 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/12 "2005-10-28T05:45:46Z")

</div>

nobuyoshi nakada wrote:

> Hi,
> 
> At Thu, 27 Oct 2005 21:07:03 +0900,  
> Trans wrote in [ruby-talk:162907]:
> 
> > A shorthand for:
> > 
> > people.every.email\_addr
> > 
> > But I like your [\*]. Hmm... I maybe able to adjust above to use [:\*]
> 
> I feel your "every" much cleaner than [\*].

So...

&nbsp;&nbsp;&nbsp;people = [person1, person2, person3]  
&nbsp;&nbsp;&nbsp;emails = people.every.email\_addr  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=\> ['test@me.com', 'test2@you.com', 'test3@us.com']  
&nbsp;&nbsp;&nbsp;# people still is [person1, person2, person3]

&nbsp;&nbsp;&nbsp;people.every!.nickname  
&nbsp;&nbsp;&nbsp;# people still is now ['Joey', 'Zeke', 'Flava Flav']

?

Zach

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [28 October 2005 12:12 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/13 "2005-10-28T12:12:04Z")

</div>

David A. Black wrote:

> I know that people.every could return  
> some kind of generator or enumerator, which could then be fed  
> "email\_addr" symbolically...

module Enumerable  
&nbsp;&nbsp;&nbsp;def every  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum, obj = self, Object.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj.define\_method :method\_missing do |name, \*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.map { |element| element.send(name, \*args) }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return obj  
&nbsp;&nbsp;&nbsp;end  
end

ary = ["john", "sylvia", "sarah"]  
ary.every.capitalize!

puts ary.join(", ") -\> John, Sylvia, Sarah

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard2](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Simon\_Strandgaard2](https://rubytalk.org/u/Simon_Strandgaard2)\
**Post date:** [28 October 2005 17:17 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/14 "2005-10-28T17:17:54Z")

</div>

Maybe this can be useful?

> **···**
>
> --  
> Simon Strandgaard
> 
> class Array  
> &nbsp;&nbsp;def xmap(\*symbols)  
> &nbsp;&nbsp;&nbsp;&nbsp;symbols.each do |symbol|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = symbol.to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "def #{s}(\*args);map{|i|i.send(:#{s}, \*args)};end"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> ary = %w(a b c d e)
> 
> ary.xmap(:upcase, :gsub)
> 
> p ary[0].upcase # "A"  
> p ary.upcase # ["A", "B", "C", "D", "E"]  
> p ary.gsub(/[bd]/, 'X') # ["a", "X", "c", "X", "e"]

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [28 October 2005 12:47 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/15 "2005-10-28T12:47:04Z")

</div>

Daniel Schierbeck wrote:

> David A. Black wrote:
> 
> > I know that people.every could return  
> > some kind of generator or enumerator, which could then be fed  
> > "email\_addr" symbolically...
> 
> module Enumerable  
> &nbsp;&nbsp;def every  
> &nbsp;&nbsp;&nbsp;&nbsp;enum, obj = self, Object.new  
> &nbsp;&nbsp;&nbsp;&nbsp;obj.define\_method :method\_missing do |name, \*args|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.map { |element| element.send(name, \*args) }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;return obj  
> &nbsp;&nbsp;end  
> end
> 
> ary = ["john", "sylvia", "sarah"]  
> ary.every.capitalize!
> 
> puts ary.join(", ") -\> John, Sylvia, Sarah
> 
> Cheers,  
> Daniel

By the way, you need the Object#define\_method if you want it to work:

class Object  
&nbsp;&nbsp;&nbsp;def define\_method(\*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(\*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [30 October 2005 10:42 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/16 "2005-10-30T10:42:06Z")

</div>

I don't like that one, because it provides no visual distinction between  
methods that act on the array as a whole and methods that map over it.  
Would be a nice place to introduce the -\> operator, though 🙂

martin

> **···**
>
> Simon Strandgaard \<neoneye@gmail.com\> wrote:
> 
> > ary = %w(a b c d e)
> > 
> > ary.xmap(:upcase, :gsub)
> > 
> > p ary[0].upcase # "A"  
> > p ary.upcase # ["A", "B", "C", "D", "E"]  
> > p ary.gsub(/[bd]/, 'X') # ["a", "X", "c", "X", "e"]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [28 October 2005 13:45 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/17 "2005-10-28T13:45:40Z")

</div>

Hi --

> Daniel Schierbeck wrote:
> 
> > David A. Black wrote:
> > 
> > > I know that people.every could return  
> > > some kind of generator or enumerator, which could then be fed  
> > > "email\_addr" symbolically...
> > 
> > module Enumerable  
> > &nbsp;&nbsp;def every  
> > &nbsp;&nbsp;&nbsp;&nbsp;enum, obj = self, Object.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;obj.define\_method :method\_missing do |name, \*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.map { |element| element.send(name, \*args) }

If you must do this, you'd probably want each rather than map there.

> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;return obj  
> > &nbsp;&nbsp;end  
> > end
> > 
> > ary = ["john", "sylvia", "sarah"]  
> > ary.every.capitalize!
> > 
> > puts ary.join(", ") -\> John, Sylvia, Sarah
> > 
> > Cheers,  
> > Daniel
> 
> By the way, you need the Object#define\_method if you want it to work:

I definitely \*don't\* want it to work 🙂 I dislike using dot syntax  
for non-dot semantics. I've never liked things like:

&nbsp;&nbsp;&nbsp;hash.where.the.key.equals(10)

even though they can usually be made to work.

I continue to struggle to understand what people find so horrible  
about ary.each {|item| .... }, ary.map {|item| ... }, and so forth.  
I'll have some more coffee and maybe I'll start to see the light....

David

> **···**
>
> On Fri, 28 Oct 2005, Daniel Schierbeck wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [28 October 2005 14:07 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/18 "2005-10-28T14:07:05Z")

</div>

David A. Black wrote:

> Hi --
> 
> > Daniel Schierbeck wrote:
> > 
> > > David A. Black wrote:
> > > 
> > > > I know that people.every could return  
> > > > some kind of generator or enumerator, which could then be fed  
> > > > "email\_addr" symbolically...
> > > 
> > > module Enumerable  
> > > &nbsp;&nbsp;def every  
> > > &nbsp;&nbsp;&nbsp;&nbsp;enum, obj = self, Object.new  
> > > &nbsp;&nbsp;&nbsp;&nbsp;obj.define\_method :method\_missing do |name, \*args|  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enum.map { |element| element.send(name, \*args) }
> 
> If you must do this, you'd probably want each rather than map there.

Nope. I wan't it to return an array of the values returned.

&nbsp;&nbsp;&nbsp;["john", "sylvia", "sarah"].every.upcase -\> ["JOHN", "SYLVIA", "SARAH"]

That's of course not a very good example. This would probably be better:

&nbsp;&nbsp;&nbsp;addresses = contacts.every.email\_addr

As opposed to

&nbsp;&nbsp;&nbsp;addresses = contacts.collect { |contact| contact.email\_addr }

But I agree that the dot syntax is bad, I was just proving that it could easily be done. This would be better:

&nbsp;&nbsp;&nbsp;addresses = contacts.every(:email\_addr)

And maybe even have a `with\_every' method:

&nbsp;&nbsp;&nbsp;contacts.with\_every(:email\_addr) do |email\_addr|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts " - " + email\_addr  
&nbsp;&nbsp;&nbsp;end

Which could be implemented this way

&nbsp;&nbsp;&nbsp;module Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def with\_every(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |element|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield element.send(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

Cheers,  
Daniel

> **···**
>
> > On Fri, 28 Oct 2005, Daniel Schierbeck wrote:

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [28 October 2005 14:47 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/19 "2005-10-28T14:47:04Z")

</div>

David A. Black wrote:

> I continue to struggle to understand what people find so horrible  
> about ary.each {|item| .... }, ary.map {|item| ... }, and so forth.  
> I'll have some more coffee and maybe I'll start to see the light....

There's nothing really wrong with them, but they are verbose in simple (and  
common) cases:

result = array.map {|element| element.transform }

The only issue is that you're saying "element" twice when you would only say  
it once if you were describing the operation to a person.

Groovy's (optional) implicit parameter is the counterpoint:

// Groovy:  
result = array.map { it.transform() }

Tangentially, I wonder if the proposed Ruby 2 block syntax makes this  
slightly more possible.

# Pseudo-Ruby 2.0:  
result = array.map -\> { it.transform }

Actually, probably less possible - IIRC the new block style is meant to be  
more like the method semantics, and so stricter on parameters.

So, anyway, I don't mind the idea of an implicit parameter, but I realise  
it's not going to happen before Ruby 3.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [28 October 2005 15:52 UTC](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938/20 "2005-10-28T15:52:09Z")

</div>

David A. Black wrote:

> I definitely \*don't\* want it to work 🙂 I dislike using dot syntax  
> for non-dot semantics. I've never liked things like:
> 
> &nbsp;&nbsp;&nbsp;hash.where.the.key.equals(10)
> 
> even though they can usually be made to work.

I sort-of agree. It's definitely a semantic we're not used to --using a  
method call to give us a "reoriented" version of the same thing --kind  
of like having Roles. I suspect this will become a more common paradigm  
over time and me may grow acustomed to it, but I'm with you in that I'd  
rather have a distinguishing indication of when that's occuring.  
Perhaps:

&nbsp;&nbsp;people:every.email\_addr

And maybe that's what Matz already has in mind. And besides, it would  
be nice to have it as a language feature becasue current  
implementations are not very efficient:

&nbsp;&nbsp;def every  
&nbsp;&nbsp;&nbsp;&nbsp;Functor.new do |op,\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.collect{ |a| a.send(op,\*args) }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

or at best

&nbsp;&nbsp;def every  
&nbsp;&nbsp;&nbsp;&nbsp;@\_\_every\_functor\_\_ ||= Functor.new do |op,\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.collect{ |a| a.send(op,\*args) }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

> I continue to struggle to understand what people find so horrible  
> about ary.each {|item| .... }, ary.map {|item| ... }, and so forth.  
> I'll have some more coffee and maybe I'll start to see the light....

Not horrible, but looking at a language like R with it's elemewise  
operations, one kind of wishes we had shorthands techinques as nice.

BTW, I have another version of #every which is even more R-like which  
DeMello helped write. But I'm searching for a another name for it. Here  
it is:

module Enumerable

&nbsp;&nbsp;# Returns an elementwise Functor designed to make R-like  
&nbsp;&nbsp;# elementwise operations possible.

> **···**
>
> #  
> &nbsp;&nbsp;# [1,2].ew + 3 #=\> [4,5]  
> &nbsp;&nbsp;# [1,2].ew + [4,5] #=\> [5,7]  
> &nbsp;&nbsp;# [1,2].ew + [[4,5],3] #=\> [[5,7],[4,5]]  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;#--  
> &nbsp;&nbsp;# Special thanks to Martin DeMello for helping to develop this.  
> &nbsp;&nbsp;#++  
> &nbsp;&nbsp;def elementwise  
> &nbsp;&nbsp;&nbsp;&nbsp;Functor.new do |op,\*args|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = args.collect do |arg|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if arg.kind\_of?(Enumerable)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ln = ( arg.length \> self.length ? self.length : arg.length )  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self[0...ln].zip(arg[0...ln]).collect{ |a,b| a.send(op,b) }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#self[0...ln].zip(arg[0...1n]).collect{ |a,b| b ?  
> a.send(op,b) : nil }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.collect{ |a| a.send(op,arg) }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.flatten! if args.length == 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;alias\_method :ew, :elementwise
> 
> end
> 
> Any suggestions?
> 
> T.

[Next page](https://rubytalk.org/t/cleaner-syntax-for-map-is-there-already-a-way-or-ruby2-idea/21938.md?page=2)
