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.
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__).
Another Pro Tip™, you can use
define_methodinside theClass.newblock to create another closure. This gives the method access to variables defined outside theClass.new:I find it to be another handy trick for defining simple mocks.
I should just ping you every time I wrote one of these. :)
I suppose, but then how would I earn this sweet sweet lobster karma? ;–)
Mmm, imaginary internet points… drools
Languages that don’t have the
#new–>#initializertrick can be a pain to work with. Especially if they don’t have ‘backup’ methods for their builder method (Like#sendvs#__send__).