[colug-432] deserialize objects in Ruby

Rob Funk rfunk at funknet.net
Wed Sep 30 11:34:26 EDT 2015


Rick Hornsby wrote:
> Thought I'd share what I came up with as a working solution,
> although its elegance is questionable.  I haven't been able to find
> any other references to this problem on the Interwebs, although it has
> to be out there.

I was seeing this as a problem with this particular implementation of
JSON deserialization, which I haven't worked with in a while. But now
it's looking like a problem with subclassing.

> The problem is that when you deserialize Dog, Dog.initialize was
> being called with the set (or a subset) of arguments first passed into
> self.json_create(*o).  Dog.initialize() calls super -
> Animal.initialize() - with that same exact set of arguments.  Animals
> and dogs have a different constructors, different properties, and thus
> need a different set (and different number) of arguments.

Why not add the proper arguments to the super call in the subclass's
initializer?

> Effectively
> 
> 	Dog.json_create(*o) -> Dog.initialize(bark) -> Animal.initialize(bark)
> 
> That doesn't work properly because the first enumerated argument to
> Animal is has_fur.

super(true, bark)
in Dog's initializer should solve that.


A subclass's initializer doesn't need to take the same arguments as
the parent class. The subclass just needs to be able to provide the
right arguments to the parent class when calling it.



More information about the colug-432 mailing list