Style question - how to represent properties

T

Tilman Sauerbeck

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
 
D

David A. Black

Hi --

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

or possible a YAML representation?


David
 
R

Robert Klemme

Tilman Sauerbeck said:
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, :eek:pt)

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
 
G

Graham Foster

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
 
F

Florian Gross

Graham said:
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?
 
G

Graham Foster

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
 
T

Tilman Sauerbeck

David A. Black said:
Hi --

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,
 
T

Tilman Sauerbeck

Graham Foster said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top