Custom serialization in Ruby

Tags:

>>>>> “M” == Minkoo Seo writes:

M> Unfortunately, Marshal::dump does not allow me to add such instance
M> variables and raises an error if such one exists.

Use #marshal_dump, #marshal_load

moulon% cat b.rb 
#!/usr/bin/ruby 
require 'logger' 
class A 
   attr_reader :a, :logger 


   def initialize 
      @a = 12 
      @logger = Logger.new(STDOUT) 
   end 


   def bar 
      @logger.info("info") 
   end 


   def marshal_dump 
      @a 
   end 


   def marshal_load(x) 
      @a = x 
      @logger = Logger.new(STDOUT) 
   end 
end 


a = A.new 
b = Marshal.load(Marshal.dump(a)) 
p b.a, b.logger.class 

 

moulon% ./b.rb 
12 
Logger 
moulon% 

Guy Decoux

p.s. 다운된 ruby-talk 와 comp.lang.ruby간의 게이트웨이는 언제살아날까요? 제가 임시 게이트웨이를 맡아서 하겠다고 올렸는데 별 반응이 없는..

Comments

3 responses to “Custom serialization in Ruby”

  1. 무책 Avatar

    아, 맨날 여쭤본다고 잊어버렸는데, 이 코드 컬러링하는 것 어떻게 하시는 거에요?

  2. MKSeo Avatar
    MKSeo

    http://blog.enargi.com/codesnippet/ 에 있는 wordpress 2.0용 codesnippet 플러그인이예요.. 무책님처럼 별도로 wordpress 등을 쓰지 않으시는 경우엔 source-highlight http://www.gnu.org/software/src-highlite/source-highlight.html 써서 html 코드 생성한다음 그걸 붙이시는 것도 좋을 듯..

  3. 무책 Avatar

    아.. 플러그인이었군요. 링크 감사합니다.

Leave a Reply

Your email address will not be published. Required fields are marked *