This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Erb Caching in a Handful
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Dr. Eric Hodel has a brief class for caching compiled ERb templates. The compiled templates aren’t stored on the filesystem—it’s not that kind of cache. It caches the template in an instance variable to prevent recompiling if you’re reusing a template.
Of particular interest to some of you might be his use of method_missing, which our goopy red hands are continually poking around in the dark for.
def method_missing(action, options = {})
super unless self.respond_to? "run_#{action}"
options.each do |attr, value|
self.class.send :attr_accessor, attr unless self.respond_to? attr
self.send "#{attr}=", value
end
return self.send("run_#{action}")
end
Hmm. You’ve got some self.class.send and some self.send. I really think this is an excellent fourteen lines of code for you kids who just got here. Any newbs want to take a stab at explaining?