"Edgardo Hames" <ehames / gmail.com> schrieb im Newsbeitrag
news:478c16ae04071907072c000169 / mail.gmail.com...
> Hi, everybody. I wondered how would you write a ResourceBundle-like
> class in Ruby. Basically, what I need is hash which is initialized by
> reading key,value pairs from a file. I've thought of this
>
> class ResourceBundle < Hash
>   def initialize(fileName)
>     #load data from fileName
>   end
> end
>
> What do you think of this approach?

Sounds reasonable enough.  I'm not 100% sure at the moment, but the lookup
is two level, isn't it?  I mean, you select a bundle via Locale and then
do the lookup along the Locale "hierarchy".  So you probably need some
parent member along these lines:

class ResourceBundle < Hash
  attr_accessor :parent

  def initialize(fileName)
    File.open(fileName) do |io|
      io.each do |line|
        /^\s*(\S+)\s*[=:]\s*(.*)$/ =~ line and self[$1]= $2
      end
    end
  end

  def [](key)
    result = super
    result = parent[key] if result.nil? && parent
    result
  end

end

Regards

    robert