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.

Alireza
  • 2,103
  • 1
  • 6
  • 19
Marcos
  • 4,796
  • 5
  • 40
  • 64
  • 1
    Clearly this is not a popular topic :) – Marcos Jan 19 '12 at 20:21
  • This might be related: http://www.ruby-forum.com/topic/1921684 There is mention of `Psych`, but how to switch from `Syck` if used internally by `Watir`? – Marcos Nov 29 '12 at 11:49
  • 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*](https://ruby-doc.org/3.2.0/Marshal.html) but it’s hard to say without sample code. – anothermh Jan 28 '23 at 23:38
  • *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. – anothermh Jan 28 '23 at 23:40

0 Answers0