Returning Gracefully

I just learned from Marcel Molina a graceful way to return an object from a method. In old days, I often have to write a method like this:

   def born(params={})
     person = params

The last line bothered me a lot, it looks ugly.

But just as usual, Rails addresses it, that is method returning. With this method, now I can modify my previous example to the following:

   def born(params={})
     returning do |person|

Basically what this method does is return the argument you give to the returning method, AFTER processing it further in the block. Simply beautiful.

For your convenience, here’s a link to the documentation. By the way it’s also very interesting to see what else rails offers in that class. They could come in handy sometimes. Here’s a link to an explanation of K Combinator. You can find tons of related links through our old dear friend google.

~ by Yi Wen on February 14, 2008.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: