On Wed, Nov 16, 2005 at 08:53:08PM +0900, George Moschovitis wrote:
> Hello all,
> 
> I have a yaml file that I parse like this
> 
> yaml = YAML.parse(yaml_str)
> 
> the 'yaml_str' variable holds the serialized version of an Object.
> Lets say this object hase 2 attributes, 'title' and 'body'.
> 
> How can I access them from the 'yaml' variable. Ie I want an imaginary
> method 'lookup':
> 
> yaml.lookup['title'] # => returns the title value.

require 'yaml'
o = Object.new
o.instance_eval{ @title = "foo"; @body = "bar" } 
s = YAML.dump(o) # => "!ruby/object \nbody: bar\ntitle: foo\n"

# for instance
def add_lookup(obj)
  def obj.lookup(x); instance_variable_get "@#{x}" end
end

o2 = YAML.load(s)
add_lookup(o2)
o2.lookup "title" # => "foo"
o2.lookup "body" # => "bar"
[RUBY_VERSION, RUBY_RELEASE_DATE] # => ["1.8.3", "2005-09-24"]

-- 
Mauricio Fernandez