Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created October 26, 2009 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/219137 to your computer and use it in GitHub Desktop.
Save mizchi/219137 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
# 今までのインスンタンス変数を引き継ぎながら
# オブジェクトにインスタンス変数を追加するには
# どのようしたらいいでしょうか
class DataObj
attr_accessor :ver,:a,:b,:c
def initialize(obj) #
@ver=0
@a,@b,@c=0,1,2
end
end
class DataObj_1 < DataObj
attr_accessor :d,:e,:f # 変数を追加
def initialize(obj)
super
@ver=1
@a,@b,@c=obj.a,obj.b,obj.c
@d,@e,@f=3,4,5
end
end
current_version=1
a=File.open(file_name, "rb"){|f| Marshal.load(f) } # ここでa はDataObjのインスタンスだとする
a.c="hello" #この変更の状態を引き継ぐ
a=DataObj_1.new(a) if a.ver < current_version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment