Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

ruby - how to serialize an object using TCPServer inside?

In an effort to speed up frequently repeated runs of a particular script in my chain, I started serializing to disk custom objects that otherwise take too much time to create aggregately.

Using built-in Yaml and/or Marshal.

Yaml serializes fine to a seemingly healthy text file but produces the following error when trying to deserialize:

b2 = YAML::load(File.open("browserObj.yaml", 'r'))
Syck::TypeError: Invalid Regular expression: "/\A\s*
        ([a-zA-Z][-+.a-zA-Z\d]*):          ...and many more strange lines 

However even trying to save to a binary file via Marshal errors:

puts File.open("browserObj.bin", 'w').write Marshal::dump($browser)  
TypeError: can't dump TCPServer

# Marshal::dump($browser, File.open("browserObj.bin", 'wb'))  # same error

By deliberately not doing $browser.close at the end I have the option to keep this TCPServer alive and running after the lifetime of my Ruby script.

Any thoughts on how I can get away with this? I promise upon successful reloading to double-check the validity of any sockets/inner objects and simply re-initialize a whole new object if I have to.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...