Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
class Object def self.once(sym) alias_method sym.to_s + "_orig", sym define_method sym do |*args| @values ||= {} if @values.has_key? sym return @values[sym] else value = self.send("#{sym.to_s}_orig", *args) @values[sym] = value return value end end end end class Example def m rand end once :m def m2(foo) return "#{foo}" end once :m2 end e = Example.new e2 = Example.new puts e.m puts e2.m puts e.m puts e2.m puts e2.m puts e2.m puts e.m2("hej") puts e2.m2("warez") puts e.m2("uv") puts e2.m2("fluff")
From the Design Piracy series on my blog: