Robert Klemme <shortcutter / googlemail.com> wrote:

> There is no need for delegation here: you only need one instance and the
> way through Hash or OpenStruct seems a detour here - at least if the set
> of fields is fixed.  Then you can do
> 
> HFSFile = Struct.new :name, :modification_date do
>    def self.parse(s)
>      o = new
>      s.scan /\s*([\w\s]+):\s*([^,]+),?/ do
>        field = normalize($1)
>        val = $2.strip
>        o[field] = val
>      end
>      o
>    end
> 
>    def self.normalize(k)
>      k.strip.gsub /\s+/, '_'
>    end
> end
> 
> Now you can do:
> 
> irb(main):033:0> HFSFile.parse "name: the_name_value, modification date:
> the date"
> => #<struct HFSFile name="the_name_value", modification_date="the date">
> irb(main):034:0>
> 
> You'll get a NameError if you parse fields from the String that are not
> present in the struct.  If you need to cope with additional fields you
> need to either ignore them or store them in an additional Hash member.
> You can use HFSFile.members (or just members in method parse()) to test
> for field presence.

OK, i see it's really fine of u helping that way !

in fact i do have a number of keys (18).

and a new prob arroses : i'd like to add new keys depending on the file
type (the input is a file path).

if the file path refers to an alias file (Mac OS X) one of the keys ( a
boolean) give me that allready but, in that case, i'd like to add a new
key :alias_broken (the original item of the alias has been deleted) i
suppose i could do that easily :

self.alias_broken = true|false

and in ruby generally the methods returning a boolean are named with an
ending "?", a good habit.

obiously, i know when this is a boolean because the string value is
"true"|"false".

-- 
 ? Je suis s?r que la ga?t? ambigu? du dr?le de gar?on qui a bu du 
whisky et fait le z?bre sur un vieux ca?man ? No?l dans le ca?on, a ?t?
b?n?fique ? l'?me du po?te bl?me, ainsi qu'? son c?ur & c?tera ! ? 
 (? Jean-Paul Blanc)