Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

The Methodphitamine: A Better Symbol#to_proc?

By Peter Cooper / August 11, 2007

Bountyshirt
(from one of Bounty Hunter Inc's great t-shirts - yes, you can buy this!)

Jay Philips has stumbled across an interesting technique that like Symbol#to_proc, allows you to define block actions more succinctly for methods like map and sort_by. It goes one step beyond Symbol#to_proc's goodness, however, and makes things even tighter. He calls it the Methodphitamine. Consider this typical Ruby on Rails example to fetch a list of capitalized last names for all users in a database:

User.find(:all).map{|x| x.contacts.map{|y| y.last_name.capitalize }}

Using Symbol#to_proc, you end up with something like:

User.find(:all).map{|x|x.contacts.map(&:last_name).map(&:capitalize)}

But with the "Methodphitamine", we get:

User.find(:all).map &its.contacts.map(&its.last_name.capitalize)

Learn more about it in Jay's blog post.

Comments

  1. Ramon Leon says:

    This is an old well known Smalltalk hack.

  2. Johan Tam says:

    It's tighter, but hard to understand, not as straightforward as the previous one.

  3. Shadowfiend says:

    Out of curiosity, what does it look like in Smalltalk?

  4. Paul Ireland says:

    I honestly could care less what other languages have used this. The fact that it is here and that I can use it... well all I have to say is: WooHoo!

  5. Sam Aaron says:

    I'm not a big fan of the to_proc hack in the first place. To my eyes, it's ugly, and Methodphitamine is even more ugly. I believe that there's a balance to be struck between readability and succinctness. I think that the Ruby philosophy (or way) leans more towards the readability side of the spectrum. Check out Matz's essay in the new O'Reilly book 'Beautiful Code' where he likens code to an essay. i.e. essays generally have messages, but the message is mostly meaningless unless it can be read and understood by other people. It's a good read.

    Anyway, I would much rather break the line up into a couple of lines - even using a do end block. Also, I'd use more appropriate variables than x and y (I'd probably also use a comment too...):

    User.find(:all).map do |user|
    user.contacts.map{|contact| contact.last_name.capitalize}
    end

    I'm happy to sacrifice a little succinctness for readability any day (although not to the extent of COBOL or Applescript ;-) )

  6. Sam Aaron says:

    With a little sobering time, and perspective, I think that the Methodphitamine option is actually not at all uglier than the to_proc hack.

    However, one important issue is - to what extent should we use hacks and tricks like this on code that other people are likely to be reading and maintaining. Especially in cases where these people haven't met said trick or hack.

    Maybe I'm just being a boring old spoilsport. Truth is, when read aloud, the Methodphitamine hack sounds more like English. Yet the standard Ruby way is more readable to me (and probably the average Ruby hacker) simply because it's a pattern I'm used to reading.

  7. Adam Sanderson says:

    I actually think that the first form is just fine. There is less magic, which makes it easier for everyone, and at the same time is still about as short as the other options. It's great to know when you can do some of these tricks, but use the uglier ones sparingly. All the same, pretty damn ingenious ;)

Other Posts to Enjoy

Twitter Mentions