P
Patrick Li
I noticed this this afternoon:
Unmarshaling a marshaled object, makes all dynamically created methods
private.
#Here's a class that dynamically creates the method MyPage#print()
class MyPage
def initialize
MyPage.class_eval do
def print
puts @a
end
end
end
end
#Test the class
page = MyPage.new
page.print #prints nil
#Marshal the object
File.open("object.obj","w") do |f|
Marshal.dump(page, f)
end
#UnMarshal the object, and try printing again
page = nil
File.open("object.obj","r") do |f|
page = Marshal.load(f)
end
page.print
#gives me:
#private method `print' called for #<MyPage:0x27af394 @a=3>
(NoMethodError)
Unmarshaling a marshaled object, makes all dynamically created methods
private.
#Here's a class that dynamically creates the method MyPage#print()
class MyPage
def initialize
MyPage.class_eval do
def print
puts @a
end
end
end
end
#Test the class
page = MyPage.new
page.print #prints nil
#Marshal the object
File.open("object.obj","w") do |f|
Marshal.dump(page, f)
end
#UnMarshal the object, and try printing again
page = nil
File.open("object.obj","r") do |f|
page = Marshal.load(f)
end
page.print
#gives me:
#private method `print' called for #<MyPage:0x27af394 @a=3>
(NoMethodError)