# Arrray#to\_proc

**URL:** https://rubytalk.org/t/arrray-to-proc/28598
**Category:** ruby-talk
**Created:** [1 July 2006 12:27 UTC](https://rubytalk.org/t/arrray-to-proc/28598 "2006-07-01T12:27:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Matthew\_Harris](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Matthew\_Harris](https://rubytalk.org/u/Matthew_Harris)
#### Post date: [1 July 2006 12:27 UTC](https://rubytalk.org/t/arrray-to-proc/28598/1 "2006-07-01T12:27:33Z")

</div>

I don't know if this was posted previously, but I had the idea of a  
Array#to\_proc, an example would be:

[9, 19, 29].map(&[:succ, :to\_s, :reverse])  
=\> ["01", "02", "03"]

I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
but I think using the Array#to\_proc solution is cleaner.

Would this actually prove useful? It would seem that using the normal  
block form would be just as much typing, though.

> **···**
>
> --  
> Matt

---

<div class="post-metadata">

### Author: ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)
#### Post date: [1 July 2006 15:25 UTC](https://rubytalk.org/t/arrray-to-proc/28598/2 "2006-07-01T15:25:18Z")

</div>

This could very soon start to look like line noise:

e.g.  
[9, 19, 29].map(&%w{succ to\_s reverse})

Symbol#to\_proc is one thing (although I don't like it), Array#to\_proc is too out there, IMO.

> **···**
>
> On Jul 1, 2006, at 8:27 AM, Matthew Harris wrote:
> 
> > I don't know if this was posted previously, but I had the idea of a  
> > Array#to\_proc, an example would be:
> > 
> > [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> > =\> ["01", "02", "03"]
> > 
> > I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
> > but I think using the Array#to\_proc solution is cleaner.
> > 
> > Would this actually prove useful? It would seem that using the normal  
> > block form would be just as much typing, though.
> > 
> > -- Matt

---

<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: [1 July 2006 15:51 UTC](https://rubytalk.org/t/arrray-to-proc/28598/3 "2006-07-01T15:51:36Z")

</div>

Matthew Harris wrote:

> I don't know if this was posted previously, but I had the idea of a  
> Array#to\_proc, an example would be:
> 
> [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> =\> ["01", "02", "03"]
> 
> I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
> but I think using the Array#to\_proc solution is cleaner.
> 
> Would this actually prove useful? It would seem that using the normal  
> block form would be just as much typing, though.

Well, unless Array's got something else to do with #to\_proc, it doesn't  
seem to me a dab things per say. What might be more intersting though  
is what I suggsed in the Symbol.to\_proc is beautiful thead combined  
with a way to force +self+ to be passed rather then the result. It  
would be something like:

&nbsp;&nbsp;[9, 19, 29].map.succ!.to\_s!.reverse  
&nbsp;&nbsp;=\> ["01", "02", "03"]

Where the ! is forcing self to be returned rather then the regular  
result. I'm not even sure this would conflict with the defintion of  
#succ! either. Altghough it could conflict with other defintions of  
bang methods that don't return self (rare but out there). Is ther an  
alternate notation?

Oh. While were at it:

&nbsp;&nbsp;class Hash  
&nbsp;&nbsp;&nbsp;&nbsp;def to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Proc.new { |o|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each {|k,v| o.\_\_send\_\_("#{k}=", v)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;end  
end

Usage:

&nbsp;&nbsp;class X  
&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor :a, :b  
&nbsp;&nbsp;&nbsp;&nbsp;def initialize( &blk )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blk[self]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;h = {:a=\>1, :b=\>2}  
&nbsp;&nbsp;X.new( &h )

Of course, it's still no as good as uniform parameters, eg no special  
form for blocks. ☹

T.

---

<div class="post-metadata">

### Author: ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)
#### Post date: [1 July 2006 16:58 UTC](https://rubytalk.org/t/arrray-to-proc/28598/4 "2006-07-01T16:58:15Z")

</div>

Matthew Harris wrote:

> I don't know if this was posted previously, but I had the idea of a  
> Array#to\_proc, an example would be:
> 
> [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> =\> ["01", "02", "03"]
> 
> I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
> but I think using the Array#to\_proc solution is cleaner.
> 
> Would this actually prove useful? It would seem that using the normal  
> block form would be just as much typing, though.

I'd rather do this:

class Proc  
&nbsp;&nbsp;&nbsp;def +(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc {|\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args = [self.call(\*args)]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.call(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;end  
end

[9, 19, 29].map( &:succ + &:to\_s + &:reverse )  
#=\> ["01", "02", "03"]

but I'm just weird like that 🙂

Is this useful for anything? It looks like it should be, but I can't think of what...

> **···**
>
> --  
> Alex

---

<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: [2 July 2006 00:52 UTC](https://rubytalk.org/t/arrray-to-proc/28598/5 "2006-07-02T00:52:08Z")

</div>

Hi,

I get a "parse error, unexpected tAMPER" with ruby 1.8.4 (2005-12-24)  
[i386-mswin32].  
What version are you using?

How about something like this:

module Enumerable  
&nbsp;&nbsp;def cascade(\*methods)  
&nbsp;&nbsp;&nbsp;&nbsp;methods.inject(self) { |ary, method| ary.map{ |x| x.send(method)}}  
&nbsp;&nbsp;end  
end

res = [9, 19, 29].cascade :succ, :to\_s, :reverse  
p res  
#=\> ["01", "02", "03"]

Unless you prefer the line noise of course 😉

Regards,  
Sean

> **···**
>
> On 7/1/06, Alex Young \<alex@blackkettle.org\> wrote:
> 
> > Matthew Harris wrote:  
> > \> I don't know if this was posted previously, but I had the idea of a  
> > \> Array#to\_proc, an example would be:  
> > \>  
> > \> [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> > \> =\> ["01", "02", "03"]  
> > \>  
> > \> I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
> > \> but I think using the Array#to\_proc solution is cleaner.  
> > \>  
> > \> Would this actually prove useful? It would seem that using the normal  
> > \> block form would be just as much typing, though.  
> > \>  
> > I'd rather do this:
> > 
> > class Proc  
> > &nbsp;&nbsp;&nbsp;def +(other)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc {|\*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args = [self.call(\*args)]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.call(\*args)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > [9, 19, 29].map( &:succ + &:to\_s + &:reverse )  
> > #=\> ["01", "02", "03"]
> > 
> > but I'm just weird like that 🙂
> > 
> > Is this useful for anything? It looks like it should be, but I can't  
> > think of what...
> > 
> > --  
> > Alex

---

<div class="post-metadata">

### Author: ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)
#### Post date: [2 July 2006 21:03 UTC](https://rubytalk.org/t/arrray-to-proc/28598/6 "2006-07-02T21:03:25Z")

</div>

Alex Young \<alex@blackkettle.org\> writes:

> Matthew Harris wrote:
> 
> > I don't know if this was posted previously, but I had the idea of a  
> > Array#to\_proc, an example would be:  
> > [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> > =\> ["01", "02", "03"]  
> > I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
> > but I think using the Array#to\_proc solution is cleaner.  
> > Would this actually prove useful? It would seem that using the  
> > normal  
> > block form would be just as much typing, though.
> 
> I'd rather do this:
> 
> class Proc  
> &nbsp;&nbsp;def +(other)  
> &nbsp;&nbsp;&nbsp;&nbsp;proc {|\*args|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args = [self.call(\*args)]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.call(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;end  
> end
> 
> [9, 19, 29].map( &:succ + &:to\_s + &:reverse )  
> #=\> ["01", "02", "03"]
> 
> but I'm just weird like that 🙂

Lovely. Too bad we can't overload "." 😉

> **···**
>
> > Alex
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

### Author: ![Takaaki\_Tateishi1](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@Takaaki\_Tateishi1](https://rubytalk.org/u/Takaaki_Tateishi1)
#### Post date: [4 July 2006 09:02 UTC](https://rubytalk.org/t/arrray-to-proc/28598/7 "2006-07-04T09:02:44Z")

</div>

Alex Young wrote:

> > [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> > =\> ["01", "02", "03"]
> 
> I'd rather do this:

...

> [9, 19, 29].map( &:succ + &:to\_s + &:reverse )  
> #=\> ["01", "02", "03"]

I also wrote similar code for my project:

&nbsp;&nbsp;[http://rubyforge.org/cgi-bin/viewvc.cgi/blockutils/?root=blockutils](http://rubyforge.org/cgi-bin/viewvc.cgi/blockutils/?root=blockutils)

That includes the other utility functions for ruby-1.9, but can't work with ruby-1.8.

Regards,

> **···**
>
> --  
> Takaaki Tateishi \<ttate@ttsky.net\>

---

<div class="post-metadata">

### Author: ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)
#### Post date: [3 July 2006 08:29 UTC](https://rubytalk.org/t/arrray-to-proc/28598/8 "2006-07-03T08:29:51Z")

</div>

Sean O'Halpin wrote:

> **···**
>
> > On 7/1/06, Alex Young \<alex@blackkettle.org\> wrote:
> > 
> > > Matthew Harris wrote:  
> > > \> I don't know if this was posted previously, but I had the idea of a  
> > > \> Array#to\_proc, an example would be:  
> > > \>  
> > > \> [9, 19, 29].map(&[:succ, :to\_s, :reverse])  
> > > \> =\> ["01", "02", "03"]  
> > > \>  
> > > \> I've seen people ask questions such as arr.map(&'meth1.meth2.meth3')  
> > > \> but I think using the Array#to\_proc solution is cleaner.  
> > > \>  
> > > \> Would this actually prove useful? It would seem that using the normal  
> > > \> block form would be just as much typing, though.  
> > > \>  
> > > I'd rather do this:
> > > 
> > > class Proc  
> > > &nbsp;&nbsp;&nbsp;def +(other)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proc {|\*args|  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args = [self.call(\*args)]  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other.call(\*args)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> > > 
> > > [9, 19, 29].map( &:succ + &:to\_s + &:reverse )  
> > > #=\> ["01", "02", "03"]
> > > 
> > > but I'm just weird like that 🙂
> > > 
> > > Is this useful for anything? It looks like it should be, but I can't  
> > > think of what...
> > > 
> > > --  
> > > Alex
> > 
> > Hi,
> > 
> > I get a "parse error, unexpected tAMPER" with ruby 1.8.4 (2005-12-24)  
> > [i386-mswin32].  
> > What version are you using?
> 
> Sorry, it assumes a working Symbol#to\_proc that's supported with that syntax... The proc chaining is what's interesting from my point of view.
> 
> --  
> Alex
