[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML to Struct

Andrew Hite

10/9/2007 11:47:00 PM

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?
--
Posted via http://www.ruby-....

5 Answers

Konrad Meyer

10/10/2007 12:49:00 AM

0

Quoth 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?

Take a look at OpenStruct.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Andrew Hite

10/10/2007 1:14:00 AM

0

> Take a look at OpenStruct.

Awesome, thanks! That looks like it should work.
--
Posted via http://www.ruby-....

ara.t.howard

10/10/2007 4:02:00 AM

0


On Oct 9, 2007, at 5:46 PM, Andrew Hite wrote:

> 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?
> --
> Posted via http://www.ruby-....
>


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://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




Andrew Hite

10/10/2007 8:01:00 PM

0

Andrew Hite wrote:
> 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)
--
Posted via http://www.ruby-....

Eric Hodel

10/10/2007 9:44:00 PM

0

On Oct 9, 2007, at 16:46 , Andrew Hite wrote:
> 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">

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars