#! /usr/bin/env ruby # attr_temporary written by Shin'ya Adzumi # version: 0.01 require 'dumper' class Object def temporary_variables (__temporary_variables).map{|tv| "#{tv.to_s}"} & instance_variables end def _pre_format unless temporary_variables.empty? then obj = self.clone obj.instance_eval do temporary_variables.each do |t| remove_instance_variable(t) end end obj else self end end private def __temporary_variables [] end end class Module private def attr_temporary(*name) name.each do |n| unless n.kind_of? Symbol then if n.respond_to? :intern then n = n.intern else raise TypeError, "#{n.inspect} is not a symbol" end end unless n.to_s =~ /^@[^@]/ then raise TypeError, "#{n.to_s} is not a instance variable" end end old_tvars = module_eval("__temporary_variables") unless (old_tvars & name) == name then new_tvars = (old_tvars | name).sort module_eval("def __temporary_variables; #{new_tvars.inspect}; end; private :__temporary_variables"); end end end module Marshal alias :_dump :dump def dump(obj,*arg) MarshalDumper::dump(obj) end module_function :_dump,:dump end if __FILE__ == $0 then class Foo attr_temporary :@foo def initialize @foo=1 end end class Bar def initialize @bar = Foo::new end end p Marshal::load(Marshal::dump(Foo::new)) p Marshal::load(Marshal::dump(Bar::new)) end