$ grep code blake

code by blake smith

the rails returning statement

All the cool Rubyists know that it’s so passé to use the return statement in a method call. You don’t want to make other Rubyists think you’re uncool by using such an unnecessary expression, do you? No, of course you don’t.

Let me tell you something else that’s also uncool. The following snippet of code, and the pattern that emerges from it:

def some_hash
  thehash = Hash.new
  thehash[:property1] = "one"
  thehash[:property2] = "two"
  thehash[:property3] = "three"
  thehash
end

What’s so uncool about this you may ask? We have two lines of routine boilerplate code that are repeated over and over again. Every time you need to build up a hash in this manner, you’re going to be instantiating a hash like this:

  thehash = Hash.new

… stuffing some data inside of it, and then explicitly calling the hash at the end of the method so that it returns from the function instead of the last evaluated property:

  thehash[:property3] = "three"
  thehash

We do this with Arrays and all sorts of other data structures:

def some_array
  thearray = Array.new
  thearray << "value1"
  thearray << "value2"
  thearray << "value3"
  thearray
end

Let me show you something that’s much cooler that you get in Rails for free. It achieves the exact some thing as above, but in a far more elegant and minimal expression:

def some_hash
  returning Hash.new do |h|
    h[:one] = "one"
    h[:two] = "two"
    h[:three] = "three"
  end
end

Oh my, that’s some beautiful code right there. Much more elegant, simple and clear. We’re cutting out two lines of boilerplate code, and making the whole thing read much cleaner. Let’s look at the same thing while rewriting the array version:

def some_array
  returning Array.new do |a|
    a << "value1"
    a << "value2"
    a << "value3"
  end
end

A true beauty!

The returning statement is both simple in use, and simple in definition:

class Object
  def returning(value)
    yield(value)
    value
  end
end

This is the kind of stuff that gets me excited about Ruby. It’s flexible enough that we can add on this kind of functionality to the base of the language without much pain or hassle. We’re not doomed to repeat ugly paradigms and write ugly code forever if we can come up with a better way of saying something, much like someone using a spoken language might. So use it! Next time you’re building up a data structure like this, bust out your awesome knowledge of the returning statement and show those other Rubyists that you’re hip and with it.



» More blog posts
« Back to home