# Arrray#to\_proc

**URL:** <https://www.ruby-forum.com/t/arrray-to-proc/96747>\
**Category:** Ruby\
**Created:** [July 1, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747 "2006-07-01T14:30:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 1, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/1 "2006-07-01T14:30:45Z")

</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.

---

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 1, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/2 "2006-07-01T17:26:24Z")

</div>

On Jul 1, 2006, at 8:27 AM, Matthew H. wrote:

> block form would be just as much typing, though.
> 
> –  
> Matt

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.

---

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 1, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/3 "2006-07-01T17:54:28Z")

</div>

Matthew H. wrote:

> 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:

[9, 19, 29].map.succ!.to\_s!.reverse  
=\> [“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:

class Hash  
def to\_proc  
Proc.new { |o|  
each {|k,v| o. **send** ("#{k}=", v)  
}  
end  
end

Usage:

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

h = {:a=\>1, :b=\>2}  
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:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 1, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/4 "2006-07-01T19:01:12Z")

</div>

Matthew H. wrote:

> block form would be just as much typing, though.

I’d rather do this:

class Proc  
def +(other)  
proc {|\*args|  
args = [self.call(\*args)]  
other.call(\*args)  
}  
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…

---

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 2, 2006, 2:53am UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/5 "2006-07-02T02:53:14Z")

</div>

On 7/1/06, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Would this actually prove useful? It would seem that using the normal  
> > end  
> > –  
> > Alex  
> > 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  
def cascade(\*methods)  
methods.inject(self) { |ary, method| ary.map{ |x| x.send(method)}}  
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

---

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 2, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/6 "2006-07-02T23:03:52Z")

</div>

Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > 
> 
> [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 “.” 😉

---

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 3, 2006, 10:31am UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/7 "2006-07-03T10:31:36Z")

</div>

Sean O’Halpin wrote:

> > > but I think using the Array#to\_proc solution is cleaner.  
> > > other.call(\*args)  
> > > 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.

---

<div class="post-metadata">

**Author:** ![mhinton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@mhinton](https://www.ruby-forum.com/u/mhinton)\
**Post date:** [July 4, 2006, 11:05am UTC](https://www.ruby-forum.com/t/arrray-to-proc/96747/8 "2006-07-04T11:05:04Z")

</div>

Alex Y. 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:

[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,
