YAML inconsistencies

I figured I’d post here before submitting a ticket, but I’m seeing some
confusing stuff when dealing with YAML now. I was using it to freeze
objects in my database, and so i had some data already around to mess
with.

I upgraded both Ruby (1.8.3 -> 1.8.4) and Rails ( -> 1.1RC1) and this
junk
started. I’ve outlined the problem in two pastes, which I’ll paste
below.

----1

This was already saved in my database, pre 1.8.3 -> 1.8.4, and pre

Rails 1.1RC1

c.lawsuit.customer_service_location
=> “— !ruby/object:Address \nattributes: \n created_on: 2006-02-24
12:30:19\n city: Alex City\n line1: 441 Saint Bernard Drive\n zip:
“35086”\n line2: ‘’\n id: “124”\n contact_detail_id: “128”\n
detail_key: Home Address\n state: AL”

And I created this YAML object from the same data.

ay
=> “— !ruby/object:Address \nattributes: \n created_on: 2006-02-24
12:30:19\n city: Alex City\n line1: 441 Saint Bernard Drive\n zip:
“35086”\n line2: “”\n id: “124”\n contact_detail_id:
“128”\n detail_key: Home Address\n state: AL\n”

You can see that line2 is treated differently between the two objects.
Also, there’s no trailing \n on the first object created.

Similarly, when I try a YAML.load of the first object on my machine
(1.8.4, 1.1RC1) I get this:

YAML.load c.lawsuit.customer_service_location
=> #<YAML::Object:0xb7986eb8 @class=“Address”,
@ivars={“attributes”=>{“line1”=>“441 Saint Bernard Drive”,
“city”=>“Alex City”, “created_on”=>“2006-02-24 12:30:19”, “line2”=>"",
“zip”=>“35086”, “id”=>“124”, “state”=>“AL”, “detail_key”=>“Home
Address”, “contact_detail_id”=>“128”}}>

But when I load the same object on the server (Ruby 1.8.2, Rails 1.0)
I get this:

YAML.load c.lawsuit.customer_service_location
=> #<Address:0x8ebc0ec @attributes={“line1”=>“441 Saint Bernard
Drive”, “city”=>“Alex City”, “created_on”=>“2006-02-24 12:30:19”,
“line2”=>"", “zip”=>“35086”, “id”=>“124”, “state”=>“AL”,
“detail_key”=>“Home Address”, “contact_detail_id”=>“128”}>

Is this a bug in Ruby? Rails? My logic?

----2

This is the same yaml string from before…

YAML.load c.lawsuit.customer_service_location
=> #<YAML::Object:0xb79cceb8 @class=“Address”,
@ivars={“attributes”=>{“line1”=>“441 Saint Bernard Drive”,
“city”=>“Alex City”, “created_on”=>“2006-02-24 12:30:19”, “line2”=>"",
“zip”=>“35086”, “id”=>“124”, “state”=>“AL”, “detail_key”=>“Home
Address”, “contact_detail_id”=>“128”}}>

Here I guess we load the Address model for the first time

a = Address.find 185
=> #<Address:0xb79076ac @attributes={“created_on”=>“2006-03-14
20:48:13”, “city”=>“Birmingham”, “line1”=>“1775 Ridgeview Lake Road”,
“zip”=>“35222”, “line2”=>"", “id”=>“185”, “contact_detail_id”=>“187”,
“detail_key”=>“Home Address”, “state”=>“AL”}>

Try to load the previous YAML string this time, and error…

YAML.load c.lawsuit.customer_service_location
NoMethodError: You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.include?
from
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active_record/base.rb:1511:in
respond_to?' from /usr/local/lib/ruby/1.8/yaml.rb:133:inload’
from (irb):5
from :0

Hi Josh - I’m pretty sure some changed were made to YAML between 1.8.2
and 1.8.4 especially regarding the ‘—’ header at the start of a YAML
string and I think the trailing newline.

Chances are the trouble you’re having is do to the Ruby upgrade rather
than Rails - you might want to run some scripts on your existing
database to weed out bits of YAML that are no longer valid and perhaps
create a migration (or just a once off script) - you might also want
to investigate installing an older version of YAML on its own to
bypass the compatibility issues.

Cheers,

-David F.