YAML on 1.8.3 question

G

George Moschovitis

Hello all,

I have a yaml file that I parse like this

yaml =3D 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'] # =3D> 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.
 
M

Mauricio Fernández

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

George Moschovitis

Ehm,

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

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

-g.


Hello all,

I have a yaml file that I parse like this

yaml =3D 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'] # =3D> returns the title value.

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

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

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

George Moschovitis

Anw,

just show that:

obj =3D YAML.load(str)
obj.ivars

does the trick

thanks,
-g.

Ehm,

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

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

-g.


Hello all,

I have a yaml file that I parse like this

yaml =3D 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 imaginar= y
method 'lookup':

yaml.lookup['title'] # =3D> returns the title value.

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

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

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

gga

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
irb> attr_accessor :x
irb> def initialize
irb> @x = 2
irb> end
irb>end
irb>a = A.new
irb>require 'yaml'
irb>b = YAML.load( YAML.dump(a) )
irb>b.x
2
 
G

George Moschovitis

Huh? You missed the point.

Probably you missed the point ;-) Read the later emails that explain
my question in more detail...

-g.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top