The “returning” method explained

This one you will find all over the Rails code. The “returning” trick is a really neat way to refactor ugly patterns, such as this one:

object = []
object << "foo" if bar?
object

Into this:

returning [] do |object|
  object << "foo" if bar?
end

So what returning does is, it creates an object using the first parameter (a new array in the above example), yeilding it to the block passed, and returns it when done. It all becomes clear in this example:

def Post.update_record(post_id)
  post = find(post_id)
  post.update_attribute(:foo, "bar")
  post
end

That ugly (well…) method finds an object, updates an attribute and saves it. But because the “save” method called by “update_attribute” just returns true or false, you will need to explicitly return the post object last in the method if you want to get it back. The returning approach just feels better for me:

def Post.update_record(post_id)
  returning find(post_id) do |post|
    post.update_attribute(:foo, "bar")
  end
end

Same lines of code in this primitive example, but it will make you look like a better Rails programmer. Here is the method in all its simple glory:

Class object
  def returning(value)
    yield(value)
    value
  end
end

Post a Comment

Your email is never shared. Required fields are marked *

*
*