[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Style question - how to represent properties

Tilman Sauerbeck

1/12/2005 10:01:00 PM

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?

TIA

--
Regards,
Tilman


7 Answers

dblack

1/12/2005 10:16:00 PM

0

Robert Klemme

1/12/2005 10:19:00 PM

0


"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

Graham Foster

1/12/2005 10:46:00 PM

0

> I'm currently writing Ruby bindings for libeet, which serializes C
> structs to disk.
I'm looking to write at utility which would write data records out to
a fixed sized structure ("database" records for an MP3 player). I was
just wondering if you can use something like Ruby (where types don't
seemed to have a clearly defined size), when I need to serialise
exact sizes of items and byte order is import. Would your binding
help in this regard? (I'm a newbie - and this looked like a Ruby
utility I could get my teeth into.)
If not - any other suggestions?
TIA
Graham

Florian Gross

1/12/2005 11:22:00 PM

0

Graham Foster wrote:

>>I'm currently writing Ruby bindings for libeet, which serializes C
>>structs to disk.
>
> I'm looking to write at utility which would write data records out to
> a fixed sized structure ("database" records for an MP3 player). I was
> just wondering if you can use something like Ruby (where types don't
> seemed to have a clearly defined size), when I need to serialise
> exact sizes of items and byte order is import. Would your binding
> help in this regard? (I'm a newbie - and this looked like a Ruby
> utility I could get my teeth into.)
> If not - any other suggestions?

Is this not what Array#pack and String#unpack do?

Graham Foster

1/13/2005 12:07:00 AM

0

> > If not - any other suggestions?
>
> Is this not what Array#pack and String#unpack do?
What I good idea. Guess its is a case of me re-reading TFM. Thanks
for the suggestion.
Graham

Tilman Sauerbeck

1/14/2005 7:06:00 PM

0

David A. Black <dblack@wobblini.net> [2005-01-14 15:31]:
> Hi --
>
> On Thu, 13 Jan 2005, Tilman Sauerbeck wrote:
>
> >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?
>
> Maybe just arrays:
>
> def to_eet_properties
> return ["name", @name],
> ["foobar", @some_fixnum, :int]
> end

Yeah, I think I'll go with this.

It's a "loose" format but I prefer this to Robert's suggestion (I
experimented with something similar to what he proposed) cause there's
less typing ;)

Thanks,

--
Regards,
Tilman


Tilman Sauerbeck

1/14/2005 7:08:00 PM

0

Graham Foster <graham@inca.freeserve.company.unitedkingdom> [2005-01-14 15:31]:
> > I'm currently writing Ruby bindings for libeet, which serializes C
> > structs to disk.
> I'm looking to write at utility which would write data records out to
> a fixed sized structure ("database" records for an MP3 player). I was
> just wondering if you can use something like Ruby (where types don't
> seemed to have a clearly defined size), when I need to serialise
> exact sizes of items and byte order is import. Would your binding
> help in this regard? (I'm a newbie - and this looked like a Ruby
> utility I could get my teeth into.)

No, it won't help you with that, since EET uses its own format.
ruby-eet is useful if you want to write EET files that are compatible
with the files some other EET-based application writes.

--
Regards,
Tilman