YAML on 1.8.3 question

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.

I used to do this with a little hack in 1.8.2, this hack does not work
on 1.8.3, I am looking for a portable way to do this.

thanks in advance for any help,

-g.

···

--
http://www.gmosx.com
http://www.navel.gr
http://www.nitrohq.com

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"]

···

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.

--
Mauricio Fernandez

George Moschovitis ha escrito:

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.

Huh? You missed the point.
YAML just serializes your object, so if your object supports a method
called title, loading it back in should too. If your object is a hash,
then you can use a hash syntax. Example:

irb
class A
attr_accessor :x
def initialize
@x = 2
end
end
a = A.new
require 'yaml'
b = YAML.load( YAML.dump(a) )
b.x

2

Ehm,

I forgot to tell you that the original object does not exist in the
current context.

so, o = YAML.load does not yield an object.

-g.

···

On 11/16/05, Mauricio Fernández <mfp@acm.org> wrote:

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

--
http://www.gmosx.com
http://www.navel.gr

Huh? You missed the point.

Probably you missed the point :wink: Read the later emails that explain
my question in more detail...

-g.

···

--
http://www.gmosx.com
http://www.navel.gr

Anw,

just show that:

obj = YAML.load(str)
obj.ivars

does the trick

thanks,
-g.

···

On 11/16/05, George Moschovitis <george.moschovitis@gmail.com> wrote:

Ehm,

I forgot to tell you that the original object does not exist in the
current context.

so, o = YAML.load does not yield an object.

-g.

On 11/16/05, Mauricio Fernández <mfp@acm.org> wrote:
> 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
>
>

--
http://www.gmosx.com
http://www.navel.gr
http://www.nitrohq.com

--
http://www.gmosx.com
http://www.navel.gr