Sat24Jul

  1. Quick Tip: Ruby Class Definition

    Everyone knows this syntax

    class Foo
    
    end

    This is cleaner when you just need to quickly define an empty class:

    Foo = Class.new

    I’m writing this ActiveRecord compliant in memory model implementation called Glamazon for this secret project I’m on at work. I used Class.new’s ability to take a block as the class body to include my library into a testable class, e.g.

    Mule = Class.new { include Glamazon::Base }

    In other Ruby code you might frequently see this used to define error classes:

    class SpecialError < StandardError
    end

    This is way cleaner:

    SpecialError = Class.new StandardError

    s