An exploratory weblog, by

Hypsometry.

XIV.

Symbolic code groups in Ruby.

This came from somewhere, I’m sure of it. I found it, used it, loved it, and here we are, years later, and I don’t have any recollection what that original source was, or who originally coined this simple idiom. Nonetheless, let me present to you a tiny little morsel of Ruby:

class Symbol
  def code(&block)
    block.call
  end
end

Include that in your project, and you can write code that does a better job of self-documenting. For example:

:filter.code do
  def reload_user
    # ..
  end
  
  def verify_humanness
    # ..
  end
end

:form.code do
  def required_status_classname
    # ..
  end
  
  def field_wrapper_classes
    # ..
  end
end

Simple Ruby syntactic sugar, helping you keep things clear and readable. Not only that, but if you’re using a block-folding text editor (such as TextMate), then you can navigate through your code a little bit more easily.