Created
October 26, 2009 23:18
-
-
Save mizchi/219137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- 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