Robert Klemme
1/12/2005 10:19:00 PM
"Tilman Sauerbeck" <tilman@code-monkey.de> schrieb im Newsbeitrag
news:20050112220310.GA4271@code-monkey.de...
> Hi,
> I'm sorry, but I couldn't think of a more concise subject line.
>
> I'm currently writing Ruby bindings for libeet, which serializes C
> structs to disk.
>
> Objects that should be serializable with EET implement the
> "to_eet_properties" method, which currently looks like this:
>
> def to_eet_properties
> {
> "name" => [@name],
> "foobar" => [@some_fixnum, :int]
> }
> end
>
> So, to_eet_properties must return some kind of enumerable object, and
> each entry has at least two attributes, a tag and value.
> Optionally, there's a third attribute. If it's omitted, some default value
> will be used instead.
>
> I'd like to improve the way the user enters these properties.
> The hash-of-arrays I use currently doesn't feel like the right way to do
> this, is there a better way?
>
> I thought about having the hash accept a single value as a value, too:
> {"name" => @name}
> but I don't know whether it's a good idea to have it use an array in
> some situations and a single value in another.
>
> Any recommendations?
How about:
PropertyInfo = Struct.new(:value, :type, :opt)
class PropertyClassInfo < Hash
def prop(name, value, type = :string, opt = nil)
self[name] = PropertyInfo.new value, type, opt
self
end
end
def to_eet_properties
PropertyClassInfo.new.
prop("name", @name).
prop("foobar", @some_fixnum, :int)
end
Just a spontaneous thought...
Regards
robert