How to represent an Array in YAML Test fixtures

All,

I have an Active Record column which I serialize into a Array of
Hashes, like below:

Model:
serialize :sizes

which, when accessed produces and structure like:
[{:quantity=>“1”, :size=>1}, {:quantity=>“2”, :size=>2},
{:quantity=>“2”, :size=>3}]

this gets converted into YAML when stored in the DB.

I am having problems representing this in my YAML test fixtures.

I have tried the following:

sizes: ‘— \n- :quantity: “1”\n :size: 1\n- :quantity: “2”\n
:size: 2\n- :quantity: “2”\n :size: 3\n’

and

sizes: <%= [{:quantity=>“1”, :size=>1}, {:quantity=>“2”, :size=>2},
{:quantity=>“2”, :size=>3}].to_yaml %>

to no avail.

Does anybody how one might represent such an array in YAML?

quantity:

  • 1
  • 2
  • 3

– Marcus B.

To those who come along, I found the following solution:

sizes: [{!ruby/sym quantity: ‘5’, !ruby/sym size: 1}, {!ruby/sym
quantity: ‘8’, !ruby/sym size: 2}, {!ruby/sym quantity: ‘8’, !ruby/sym
size: 3}]

see:
http://yaml4r.sourceforge.net/cookbook/#simple_inline_array
http://yaml4r.sourceforge.net/cookbook/#symbols

More generally, you can find out the yaml format
for any data structure from the console:

planb:/~ $ ./script/console
Loading development environment.

b = %w(eeny meeny miney mo)
=> [“eeny”, “meeny”, “miney”, “mo”]
require ‘yaml’
=> false
puts b.to_yaml


  • eeny
  • meeny
  • miney
  • mo
    => nil

quit

(ignore the ‘false’ after the require).

On 04/09/06, Marcus B. [email protected] wrote:


Rasputin :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/

Awesome! Spent way too much time reading the YAML specification to try
to make this work for me. Thanks for posting your solution and links.

Jamie