我做了一些Ruby类动态加载/卸载/更新实验,实现了插件基础设施。我发现了几点:
我的问题是,是否有一种简单的方法可以将现有对象从旧版本“switch”创建到新版本(但不是旧版本和新版本的合并版本)?在我看来,可能的方法是在卸载/加载后重新创建对象,这不适合插件(不希望它被销毁)。
更新 :我的目的是让新版本更新现有对象,而不会将旧版本与新版本合并(例如更改数量)参数,或删除方法)。卸载然后重新加载似乎是最干净的方法,尽管你必须跟踪所有这些对象并在需要时重新创建它们。此外,昂贵的物体可能不适合重新创建。这给我留下了第二个选项,禁止意外合并发生。只要没有删除方法,没有方法签名改变,合并应该可以正常工作。
以下是我的测试程序:
$ cat test.rb
load 'v1.rb'
puts "=> 'v1.rb' loaded"
a1 = A.new
puts "=> object a1(#{a1}) created"
a1.common
a1.method_v1
load 'v2.rb'
puts '',"=> class A updated by 'v2.rb'"
a1.common
a1.method_v1
a1.method_v2
a2 = A.new
puts '',"=> object a2(#{a2}) created"
a2.common
a2.method_v1
a2.method_v2
Object.send(:remove_const, 'A')
puts '',"=> class A unloaded"
A.new rescue puts $!
puts '',"=> class A does not exist now"
a1.common
a1.method_v1
a1.method_v2 rescue puts $!
a2.common
a2.method_v1
a2.method_v2
load 'v3.rb'
puts '',"=> 'v3.rb' loaded"
a1.common
a1.method_v1
a1.method_v2 rescue puts $!
a1.method_v3 rescue puts $!
a2.common
a2.method_v1
a2.method_v2
a2.method_v3 rescue puts $!
a3 = A.new
puts '',"=> object a3(#{a3}) create"
a3.common
a3.method_v1 rescue puts $!
a3.method_v2 rescue puts $!
a3.method_v3
示例输出:
$ ruby test.rb
=> 'v1.rb' loaded
=> object a1(#<A:0x1042d4b0>) created
#<A:0x1042d4b0>: common: v1
#<A:0x1042d4b0>: method v1
=> class A updated by 'v2.rb'
#<A:0x1042d4b0>: common: v2
#<A:0x1042d4b0>: method v1
#<A:0x1042d4b0>: method v2
=> object a2(#<A:0x1042cec0>) created
#<A:0x1042cec0>: common: v2
#<A:0x1042cec0>: method v1
#<A:0x1042cec0>: method v2
=> class A unloaded
uninitialized constant A
=> class A does not exist now
#<A:0x1042d4b0>: common: v2
#<A:0x1042d4b0>: method v1
#<A:0x1042d4b0>: method v2
#<A:0x1042cec0>: common: v2
#<A:0x1042cec0>: method v1
#<A:0x1042cec0>: method v2
=> 'v3.rb' loaded
#<A:0x1042d4b0>: common: v2
#<A:0x1042d4b0>: method v1
#<A:0x1042d4b0>: method v2
undefined method `method_v3' for #<A:0x1042d4b0>
#<A:0x1042cec0>: common: v2
#<A:0x1042cec0>: method v1
#<A:0x1042cec0>: method v2
undefined method `method_v3' for #<A:0x1042cec0>
=> object a3(#<A:0x1042c3f8>) create
#<A:0x1042c3f8>: common: v3
undefined method `method_v1' for #<A:0x1042c3f8>
undefined method `method_v2' for #<A:0x1042c3f8>
#<A:0x1042c3f8>: method v3
以下是A类的3个版本:
$ cat v1.rb
class A
def common
puts "#{self}: common: v1"
end
def method_v1
puts "#{self}: method v1"
end
end
$ cat v2.rb
class A
def common
puts "#{self}: common: v2"
end
def method_v2
puts "#{self}: method v2"
end
end
$ cat v3.rb
class A
def common
puts "#{self}: common: v3"
end
def method_v3
puts "#{self}: method v3"
end
end
答案 0 :(得分:1)
简而言之,如果没有严重的黑客攻击,就没有办法做到这一点。我建议你做的是创建一个to_serialized
方法,该方法返回initialize
方法接受的数组以获得相同的状态。如果您只想复制所有实例变量,可以执行以下操作:
class A
def initialize(instance_variables)
instance_variables.each do |key, value|
self.instance_variable_set(key, value)
end
end
def to_serialized
iv = {}
self.instance_variables.each do |key|
iv[key] = self.instance_variable_get(key)
end
end
end
要重新加载方法,您可以这样做:
obj_state = object.to_serialized
Object.send(:remove_const, 'A')
load 'file.rb'
object = A.new(obj_state)
请注意,这不会嵌套,因此如果实例变量引用的任何对象也重新加载,则需要自己“序列化”它们。
答案 1 :(得分:1)
显然,使用新的类定义完全替换类定义存在危险,无论是合并新版本还是删除旧版本并期望对象自动更新。这种危险是因为旧版本的对象可能处于新版本的无效状态。 (例如,在initialize
方法中初始化类的新版本的实例变量可能尚未由旧版本定义,但可能存在比此更微妙的错误)。 无论你如何关闭它,都需要谨慎(以及精心策划的升级路径)。
鉴于你知道你正在升级的版本是什么样的(无论如何你都需要升级),让新版本的类从旧版本的类中删除不需要的方法已经很简单了。 :
class A
remove_method :foo
end
当你说重新定义一个方法以获取不同数量的参数时,我不确定你在谈论什么。它适用于我:
class A
def foo a
a
end
end
ainst=A.new
p(ainst.foo 1) rescue puts($!)
p(ainst.foo 1,2) rescue puts($!)
class A
def foo a,b
[a,b]
end
end
p(ainst.foo 1) rescue puts($!)
p(ainst.foo 1,2) rescue puts($!)
你唯一不能做的事情(AFAIK)就是改变班级的超类。这是在您第一次定义类时定义的,并且您不允许更改它(尽管您可以再次指定相同的祖先类)。
class A < Object
end
class A < Object
end
class A < String #TypeError: superclass mismatch for class A
end