# More Proc Beauty

**URL:** <https://www.ruby-forum.com/t/more-proc-beauty/59908>\
**Category:** Ruby\
**Created:** [May 8, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/more-proc-beauty/59908 "2006-05-08T13:47:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [May 8, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/more-proc-beauty/59908/1 "2006-05-08T13:47:13Z")

</div>

The madness continues! This is getting out of hand!

First, a recap:

class Symbol  
def to\_proc  
# leave out the `&block’ unless  
# you’re using Ruby 1.9  
proc{|obj, \*args, &block| obj.send(self, \*args, &block) }  
end  
end

# now we can do stuff like this:

%w{dog cat monkey}.collect(&:upcase) =\> [“DOG”, “CAT”, “MONKEY”]

# this is also mainly 1.9 stuff

module Enumerable  
class Enumerator  
def &(name)  
each(&name)  
end  
end  
end

# This is cool, but it looks kind of weird:

%w{cat dog monkey}.collect&:reverse =\> [“tac”, “god”, “yeknom”]

And here’s the new stuff:

# now, this is really, really stupid in a real-world app.

# but hey, it’s still pretty cool!

def method\_missing(name, \*args)  
return name  
end

# now just look at all the coolness!

%w{dog cat monkey}.collect&upcase =\> [“DOG”, “CAT”, “MONKEY”]

# You can even put spaces in!

%w{dog cat monkey}.collect & reverse =\> [“tac”, “god”, “yeknom”]

# this will of course break if the variable is defined

upcase = “foobar”  
%w{dog cat monkey}.collect & upcase =\> TypeError

I can’t believe I used to write in PHP!

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [May 8, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/more-proc-beauty/59908/2 "2006-05-08T19:09:27Z")

</div>

Daniel S. wrote:

> The madness continues! This is getting out of hand!  
> Scarlet fever. Tell me, is there a little red ping-pong ball floating  
> in your peripheral vision that you can’t ever seem to stare directly  
> at? In time, it will become your dearest friend.
> 
> # now, this is really, really stupid in a real-world app.
> 
> # but hey, it’s still pretty cool!
> 
> def method\_missing(name, \*args)  
> return name  
> end
> 
> # now just look at all the coolness!
> 
> %w{dog cat monkey}.collect&upcase =\> [“DOG”, “CAT”, “MONKEY”]  
> Or maybe you could move your method\_missing into Enumerator. Then you  
> could do:

%w{dog cat monkey}.collect.upcase =\> [“DOG”, “CAT”, “MONKEY”]

\_why

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [May 8, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/more-proc-beauty/59908/3 "2006-05-08T19:52:54Z")

</div>

why the lucky stiff wrote:

> Or maybe you could move your method\_missing into Enumerator. Then you  
> could do:
> 
> %w{dog cat monkey}.collect.upcase =\> [“DOG”, “CAT”, “MONKEY”]

Suuuure, if you want to do it the _easy_ way! 😛

Hehe, I was merely mesmerized by how easy it was to implement something  
like that.

Cheers,  
Daniel
