how to serialize an object using TCPServer inside?
Asked Answered
V

0

1

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*\n        ([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.

Vibraharp answered 12/1, 2012 at 16:10 Comment(4)
Clearly this is not a popular topic :)Vibraharp
This might be related: ruby-forum.com/topic/1921684 There is mention of Psych, but how to switch from Syck if used internally by Watir?Vibraharp
I don’t think there’s much interest in this question because you haven’t provided a minimal reproducible example. You’re speaking about specific objects that generate specific errors and asking for help. But how can anyone help when we don’t have the actual objects to work with? You’re possibly running into the issue where Some objects cannot be dumped: if the objects to be dumped include bindings, procedure or method objects, instances of class IO, or singleton objects, a TypeError will be raised but it’s hard to say without sample code.Flocculus
You have to break down the problem into the simplest reproducible example that will generate these errors. You’re presuming we'll create our own TCPServer and try serializing or marshaling them; why would we do that? We have no way to know if they represent of your use-case. I don’t look at answers as being a dialogue so that you can respond and say “That doesn’t cover my specific use-case, it’s actually xyz.” The onus is on you to provide the data needed for an answer in your question, and right now this question does not contain enough detail to provide an answer.Flocculus

© 2022 - 2024 — McMap. All rights reserved.