1. 8

  2. 9

    Another Pro Tip™, you can use define_method inside the Class.new block to create another closure. This gives the method access to variables defined outside the Class.new:

    bar = :hello
    z = Class.new {
      define_method(:omg) { bar }
    z.new.omg # => :hello

    I find it to be another handy trick for defining simple mocks.

    1. 4

      I should just ping you every time I wrote one of these. :)

      1. 5

        I suppose, but then how would I earn this sweet sweet lobster karma? ;–)

        1. 2

          Mmm, imaginary internet points… drools

    2. 1

      Languages that don’t have the #new –> #initializer trick can be a pain to work with. Especially if they don’t have ‘backup’ methods for their builder method (Like #send vs #__send__).