Wednesday 15 May 2013

ruby - Calling initialize when loading an object serialized with YAML -


Is it possible to force Ruby to call an initial method when using YAML.load_file? For example, I want to call the method to provide value, do not serialize the variable I know that I can factor code into a different method and call YAML.load_file after calling it But I was wondering if there is one more great way to handle this problem.

I do not think you can, because the code you add to is actually specific to the classified class So, you should consider adding convenience to the class. For example, if you want to deserialize the foo class, you can add a class method like:

  class foo def self.from_yaml (yaml) foo = YAML :: load (yaml) # foo Edit the object here foo end end myFoo = Foo.from_yaml ("myFoo.yaml")  

No comments:

Post a Comment