From: Hal Fulton Date: 2004-11-27T01:16:16+09:00 Subject: Re: YAML question (hi _why!) why the lucky stiff wrote: > Hal Fulton wrote: > >> For those curious, what I want to do is establish defaults for >> fields in an object, such that: >> >> 1. When I dump an object, fields that still have the default >> values will be left unspecified. >> 2. When I load an object, unspecified fields will be given their >> default values. >> > > Hi, Hal. Good for you, bringing this up again. We didn't finish this > discussion at all and the clock has struck loudly here: time to continue. Bonjour and thanks for the reply! Are you near an irc client, btw, since we seem to be talking in near- realtime? > In Hobix, I am currently using a mixin for classes which need the above. > This mixin relies upon the presence of a property_map method within > those classes, which defines the full set of instance variables which I > want to output in YAML, along with whether those fields are optional or > not. This property_map isn't clear to me yet. Why not class-level data instead of instance? I hadn't thought about the issue of required vs. optional. I would have just said: If it doesn't have a default, and it's not there, default it to nil. Ordering is also interesting; I hadn't thought of that either. > As you can see, the mixin simply defines a to_yaml_properties method, > which is a method understood by the YAML library. The > to_yaml_properties method should return an Array of instance variable > names, in the order they are output. Fine, clear enough. > As you can see, the mixin also defines 'initialize', since you'll > probably want to apply the defaults even if the object is created > programmatically. This confuses me greatly. What if a class already has an #initialize (as most do?). > You'll also need to hook yourself a YAML type for each class. In the > case of the Hobix::Entry class, I'm hooking !hobix.com,2004/entry. Argh. If I had to do this stuff every time, I'd almost rather keep using the hack I'm using. But I'll look at your examples. Maybe I just haven't seen the light yet. Thanks much, Hal