Marshal/Serialization Question

B

Bryan Richardson

Hello all,

I'm trying to Marshal some Ruby objects to file and I have a question
about parent classes. Say I have the following:

class Device
@@id = 0
@@id_map = Hash.new
@@device_map = Hash.new

def initialize(*args)
id = @@id += 1
@@id_map[self] = id
@@device_map[id] = self
end

def self.id(device)
return @@id_map[device]
end

def self.device(id)
return @@device_map[id]
end
end

class Foo < Device
def initialize
super
end
end

Now, if I create a Foo class, dump it to file and then load it again
using Marshal, should I be able to use the id and device methods in the
parent class or will the class variables not be dumped and loaded?
 
R

Robert Klemme

Hello all,

I'm trying to Marshal some Ruby objects to file and I have a question
about parent classes. Say I have the following:

class Device
@@id = 0
@@id_map = Hash.new
@@device_map = Hash.new

def initialize(*args)
id = @@id += 1
@@id_map[self] = id
@@device_map[id] = self
end

def self.id(device)
return @@id_map[device]
end

def self.device(id)
return @@device_map[id]
end
end

class Foo < Device
def initialize
super
end
end

Now, if I create a Foo class, dump it to file and then load it again
using Marshal, should I be able to use the id and device methods in the
parent class or will the class variables not be dumped and loaded?

Try it out. I'd say, most likely they are not dumped as classes are not
serialized IIRC. Class variables are evil anyway... :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top