YAML to Struct

A

Andrew Hite

Is it possible to take YAML and turn it into a Struct? I want to take
something that looks like this:

key_1: Value 1
key_2: Value 2

And turn it into an object with accessors...and I'm assuming a Struct
would be my best bet. I'm relatively new to the concepts of YAML and
the Struct class, so I'm banging my head against the wall trying to
figure this out.

Any ideas?
 
K

Konrad Meyer

--nextPart4994387.8vUXAUJMcX
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth Andrew Hite:
Is it possible to take YAML and turn it into a Struct? I want to take
something that looks like this:
=20
key_1: Value 1
key_2: Value 2
=20
And turn it into an object with accessors...and I'm assuming a Struct
would be my best bet. I'm relatively new to the concepts of YAML and
the Struct class, so I'm banging my head against the wall trying to
figure this out.
=20
Any ideas?

Take a look at OpenStruct.

=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart4994387.8vUXAUJMcX
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHDCF9CHB0oCiR2cwRAoQiAKC8s82b1nLzU+LGAHjJqrToNWEOywCdEzTU
xAZ0GrzbBc4ZlyPII4IHOU8=
=cuJS
-----END PGP SIGNATURE-----

--nextPart4994387.8vUXAUJMcX--
 
A

ara.t.howard

Is it possible to take YAML and turn it into a Struct? I want to take
something that looks like this:

key_1: Value 1
key_2: Value 2

And turn it into an object with accessors...and I'm assuming a Struct
would be my best bet. I'm relatively new to the concepts of YAML and
the Struct class, so I'm banging my head against the wall trying to
figure this out.

Any ideas?


cfp:~ > cat a.rb
require 'yaml'

class Hash
def to_struct class_name = nil
klass =
unless class_name
Struct.new *keys.map{|key| key.to_sym}
else
Struct.new class_name.to_s, *keys.map{|key| key.to_sym}
end
klass.new *values
end
end

hash = YAML.load DATA.read

struct = hash.to_struct

p struct
p struct.x
p struct.key
p struct.a

__END__
x : y
key : value
a : 42


cfp:~ > ruby a.rb
#<struct #<Class:0x40c43c> a=42, x="y", key="value">
"y"
"value"
42

a @ http://codeforpeople.com/
 
A

Andrew Hite

Andrew said:
Is it possible to take YAML and turn it into a Struct? I want to take
something that looks like this:

key_1: Value 1
key_2: Value 2

And turn it into an object with accessors...and I'm assuming a Struct
would be my best bet. I'm relatively new to the concepts of YAML and
the Struct class, so I'm banging my head against the wall trying to
figure this out.

Any ideas?

I was able to achieve this using OpenStruct and two lines of code:

data = YAML::load(yaml_data).to_hash
@item = OpenStruct.new(data)
 
E

Eric Hodel

Is it possible to take YAML and turn it into a Struct? I want to take
something that looks like this:

key_1: Value 1
key_2: Value 2

And turn it into an object with accessors...and I'm assuming a Struct
would be my best bet. I'm relatively new to the concepts of YAML and
the Struct class, so I'm banging my head against the wall trying to
figure this out.

The easiest way is to create a MyStruct and add "--- !ruby/
struct:MyStruct" to your YAML then use YAML.load. This way you can
round-trip without pain.

$ ruby
require 'yaml'

MyStruct = Struct.new :key_1, :key_2

data = MyStruct.new 'Value 1', 'Value 2'

yaml = data.to_yaml

puts yaml

newdata = YAML.load yaml

p newdata
--- !ruby/struct:MyStruct
key_1: Value 1
key_2: Value 2
#<struct MyStruct key_1="Value 1", key_2="Value 2">
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top