This is a little tune I’ve been humming a lot lately. It’s as handy as array.map(&:property). Really. You ever find yourself doing something like this?
def change_state(object_id, new_state) object = find(object_id) object.state = new_state object.save object end
Maybe not, but the pattern’s there: make an object, do something to the object, return the object. Rails’ returning method bundles this all up into joyful syntax:
def change_state(object_id, new_state) returning find(object_id) do |object| object.state = new_state object.save end end
You get the idea.
Try this one on for size (from ActionController):
def self.view_class @view_class ||= # create a new class based on the default template class and include helper methods returning Class.new(ActionView::Base) do |view_class| view_class.send(:include, master_helper_module) end end