# Alternative to the Symbol#to\_proc hack

**URL:** <https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966>\
**Category:** Ruby\
**Created:** [July 8, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966 "2006-07-08T21:42:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 8, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/1 "2006-07-08T21:42:39Z")

</div>

I’ve always been in two minds about the Symbol#to\_proc hack, it’s  
clever but adds a bit of line noise. Then I figured you can turn it  
around:

class String  
def everything\_in(e)  
e.map { |u| u.send(self) }  
end  
end

> “upcase”.everything\_in %w( i am an array )  
> =\> [“I”, “AM”, “AN”, “ARRAY”]

It’s a bit nasty because in 1.8 it can call private methods, but I  
kinda like it. And it’s not much use if you want to pass arguments  
either.

Not sure if it’s been done before either.

Ashley

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 8, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/2 "2006-07-08T22:08:10Z")

</div>

Hi –

On Sun, 9 Jul 2006, Ashley M. wrote:

> =\> [“I”, “AM”, “AN”, “ARRAY”]
> 
> It’s a bit nasty because in 1.8 it can call private methods, but I kinda like  
> it. And it’s not much use if you want to pass arguments either.

It seems a little odd to send a message to a string, and then have to  
backtrack to the left, so to speak, and realize that the receiver of  
the message is actually a message… It actually feels to me like  
it’s entering the traditional realm of the class method:

String.upcase\_all %w{ i am an array }

(Non-existent, but a typical class-method domain case.)

Of course, I’m hopelessly non-edge – I actually think that:

%w{i am an array}.map {|item| item.upcase }

is logical, reads nicely left to right, and contains no line noise 🙂

David

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 8, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/3 "2006-07-08T22:26:44Z")

</div>

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

> > end  
> > end

you could do that neatly by trapping method\_missing for String class  
methods and matching against /^.\*\_all$/ 🙂

martin

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 9, 2006, 12:21am UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/4 "2006-07-09T00:21:09Z")

</div>

On Jul 08, 2006, at 9:05 pm, removed\_email\_address@domain.invalid wrote:

> Of course, I’m hopelessly non-edge – I actually think that:
> 
> %w{i am an array}.map {|item| item.upcase }
> 
> is logical, reads nicely left to right, and contains no line noise 🙂

True, but that only makes Java look old-fashioned. I wanted to make  
Ruby look old-fashioned ;o)

Ever since I read about Higher-Order Messages at http://  
[nat.truemesh.com/archives/000537.html](http://nat.truemesh.com/archives/000537.html) I’ve been determined to find a  
way to write plain English as valid Ruby code. My goal is to find a  
neat way to express business rules in Ruby, with as little framework  
coding as possible. I think it may take a _little_ more than  
extending the String class though!

Ashley

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 9, 2006, 12:33am UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/5 "2006-07-09T00:33:48Z")

</div>

Hi –

On Sun, 9 Jul 2006, Ashley M. wrote:

> look old-fashioned ;o)  
> Hmmmm… I’m still rather fond of Ruby, I have to admit 🙂

> Ever since I read about Higher-Order Messages at  
> [http://nat.truemesh.com/archives/000537.html](http://nat.truemesh.com/archives/000537.html) I’ve been determined to find a  
> way to write plain English as valid Ruby code. My goal is to find a neat way  
> to express business rules in Ruby, with as little framework coding as  
> possible. I think it may take a _little_ more than extending the String  
> class though!

Beware the dot, though. It can give you English, but at the expense  
of Ruby style. When I see:

x = half.of.the.first.item.in(container)

I want to know, and understand, what the message “the” means to the  
return value of a call to “of” – and it isn’t easy. Of course it’s  
all legal Ruby, and can be documented… but it really sails beyond  
the horizon of what i would consider reasonable use of method-call  
syntax. In fact it’s often a case of method-call syntax being used  
for method-_name_ semantics.

David

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 9, 2006, 3:42am UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/6 "2006-07-09T03:42:20Z")

</div>

On Jul 08, 2006, at 11:31 pm, removed\_email\_address@domain.invalid wrote:

> for method-_name_ semantics.  
> David

That’s a good point I hadn’t considered. I don’t know what to think  
really. On the one had, I like code that is readable in the sense  
that you can see what it does; on the other, I like to read code  
where I can see _how it does it_. I think the former usually wins  
though, or we’d all be programming in assembly.

Rails has got a lot of people using Ruby tricks that they don’t fully  
understand (eg collecting hashes and arrays in method definitions).  
We’re just starting to use it where I work, and first developer has  
had to dive in and use Rails with only a token understanding of  
Ruby. (Maybe I’m just weird - I prefer to understand how singleton  
classes and method rewriting work before I go headfirst into crazy  
stuff like Hello World!)

It’s good that Ruby can be made so readable that you don’t _need_ to  
understand it in depth to do useful stuff, but I think it will be the  
undoing of many a newbie as they move to more ambitious projects.  
I’m crossing my fingers that Rails doesn’t become to web database  
apps what Access is to desktop database apps! (What a terrible  
thought to go to bed on…)

Ashley

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 9, 2006, 4:07am UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/7 "2006-07-09T04:07:21Z")

</div>

Hi –

On Sun, 9 Jul 2006, Ashley M. wrote:

> > all legal Ruby, and can be documented… but it really sails beyond  
> > it\*. I think the former usually wins though, or we’d all be programming in  
> > assembly.

Maybe… though much of what I like about Ruby is how those two things  
usually mesh so nicely. The serial.dot.style obscures the “how”, and  
does so unnecessarily in my opinion.

> Rails has got a lot of people using Ruby tricks that they don’t fully  
> understand (eg collecting hashes and arrays in method definitions). We’re  
> just starting to use it where I work, and first developer has had to dive in  
> and use Rails with only a token understanding of Ruby. (Maybe I’m just weird
> 
> - I prefer to understand how singleton classes and method rewriting work  
> before I go headfirst into crazy stuff like Hello World!)

What a coincidence – I wrote my book precisely for weird people like  
you who want to know what they’re doing 🙂

> It’s good that Ruby can be made so readable that you don’t _need_ to  
> understand it in depth to do useful stuff, but I think it will be the undoing  
> of many a newbie as they move to more ambitious projects.

The low entry barrier is definitely both a blessing and a curse – the  
latter in the sense that it can make people think either (a) there’s  
nothing more to learn; it’s all quickly available, or (b) there might  
be more to learn but the easy part is over and the rest will be really  
hard. Happily, neither is true; the truth is that there’s a lot left  
to learn and understand, and if done correctly it’s quite accessible.

> I’m crossing my fingers that Rails doesn’t become to web database  
> apps what Access is to desktop database apps! (What a terrible  
> thought to go to bed on…)

When I wrote “Ruby for Rails”, I was thinking a lot about the  
precedent of the role of CGI programming in the Perl world – namely,  
that it’s one of the greatest manifestations of Perl and, at the same  
time, a bottomless swamp of misunderstanding and misuse of the  
language. I wanted to make sure nothing similar, comparable, or even  
analogous happened as between Ruby and Rails. Mind you, I don’t think  
we were ever in danger of wholesale Perl/CGI-style script kiddiness.  
But there’s definitely scope for either knowing or not knowing what  
one is doing, and like you I tend to prefer and encourage the former  
🙂

David

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 9, 2006, 5:54am UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/8 "2006-07-09T05:54:43Z")

</div>

Ashley M. wrote:

> I’m crossing my fingers that Rails doesn’t become to web database apps  
> what Access is to desktop database apps! (What a terrible thought to  
> go to bed on…)  
> Access is great until your database gets a byte or 2 bigger than 2 GB.  
> 🙂

–  
M. Edward (Ed) Borasky

> **[世界杯竞猜彩票-世界杯官网竞猜（集团）有限公司](http://www.linuxcapacityplanning.com/)**
>
> 世界杯竞猜彩票一直以来遵守商业道德，世界杯官网竞猜坚持诚信为本，保证诚实经营。世界杯竞猜彩票先后成为安徽省电子商务协会理事单位、安徽省网商协会会员单位、合肥市电子商务协会理事单位、合肥市服务外包行业协会会员单位、安徽省云计算产业促进会会员单位。世界杯官网竞猜一直保持良好的技术及服务态度，更好地为客户服务。

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [July 9, 2006, 6:43am UTC](https://www.ruby-forum.com/t/alternative-to-the-symbol-to-proc-hack/102966/9 "2006-07-09T06:43:20Z")

</div>

Ashley M. wrote:

> > “upcase”.everything\_in %w( i am an array )  
> > =\> [“I”, “AM”, “AN”, “ARRAY”]

That’s nice for map, but the to\_proc hack can apply to ANY method. For  
example:  
%w( i am an array ).sort\_by(&:reverse)

Daniel
