When modifying a view, we check the HTML output very often. Sometime refreshing browser is expensive because of the complex controller logic like fetching external resources. We can stub data at the very beginning but we still have to test with real data in some point of time. I wrote a simple rails helper, to memorize all instance variables defined before rendering a view. Then it can be reused for rendering next time.
The Helper
1234567891011121314151617
defiv_cacheraise"Block is expected"unlessblock_given?returnyieldifRails.env!='development'id="#{self.controller_path}##{self.action_name}"$iv_cache.try:delete,idifparams.has_key?(:no_iv_cache)if$iv_cache&&$iv_cache.has_key?(id)$iv_cache[id].each{|k,v|self.instance_variable_set(k,v)}elseyield$iv_cache||={}$iv_cache[id]=self.instance_variables.each_with_object({}){|key,h|h[key.to_s]=self.instance_variable_get(key)}endend