From: 7stud -- Date: 2009-10-08T03:06:01+09:00 Subject: Re: Ruby Marshal unexpected results Aaron Vegh wrote: > Hi there, > For a Rails project, I'm trying to serialize some data in a hash, using > Marshal. However, this data is getting serialized in unexpected ways. > I'm not sure if there's a problem with Marshal, my data, or me. > > In Rails, I have a parameter that is a hash of data from a form: > > {"305"=>"Complete trim detail at top of stringer where basement > staircase meets first floor level. ", > "25"=>"The vertical joint between the two adjacent masonry wythes at > the ... do not appear to be bonded or tied together. Cracking of the > mortar is resulting. \t\t\t\t\t\t > ", > "728"=>"The mantle above factory built fireplace in the family room has > been installed in an un-workmanlike manner. Gaps between the mantle and > wall are too excessive. "} > > In my controller, I serialize the data with Marshal: > > data = Marshal.dump(p) > > But when I save it, I get an error. Here's the beginning of it: > > SQLite3::SQLException: unrecognized token: > "'C:HashWithIndifferentAccess{"305"": UPDATE "reports" SET > "updated_at" = '2009-10-07 14:49:26', "data" = > 'C:HashWithIndifferentAccess..." > > I logged the result of that Marshal call, and this is what I got: > > C:HashWithIndifferentAccess305"`Complete trim detail at top of stringer > where basement staircase meets first floor level. "25"?The vertical > joint between the two adjacent masonry wythes at the ... do not appear > to be bonded or tied together. Cracking of the mortar is resulting. > 728"?The mantle above factory built fireplace in the family room has > been installed in an un-workmanlike manner. Gaps between the mantle and > wall are too excessive. > > Which to me doesn't look at all like what Marshal should be producing. > So what gives? > > Thanks, > Aaron. require 'rubygems' require 'sqlite3' db = SQLite3::Database.new( "mytest.db" ) db.execute("drop table mytable") db.execute("create table mytable (data TEXT)") h = { "305"=>"Complete trim detail at top of stringer where basement staircase meets first floor level. ", "25"=>"The vertical joint between the two adjacent masonry wythes at the ... do not appear to be bonded or tied together. Cracking of the mortar is resulting. \t\t\t\t\t\t ", "728"=>"The mantle above factory built fireplace in the family room has been installed in an un-workmanlike manner. Gaps between the mantle and wall are too excessive. " } str = Marshal.dump(h) p str --output:-- "\004\b{\b\"\b305\"`Complete trim detail at top of stringer where basement\nstaircase meets first floor level. \"\a25\"\001\241The vertical joint between the two adjacent masonry wythes at\nthe ... do not appear to be bonded or tied together. Cracking of the\nmortar is resulting. \t\t\t\t\t\t\n\"\b728\"\001\242The mantle above factory built fireplace in the family room has\nbeen installed in an un-workmanlike manner. Gaps between the mantle and\nwall are too excessive. " command = "insert into mytable (data) values ('#{str}')" db.execute(command) command = "insert into mytable (data) values ('hello world')" db.execute(command) rows = db.execute("select * from mytable") p rows --output:-- [["\004\b{\b\"\b305\"`Complete trim detail at top of stringer where basement\nstaircase meets first floor level. \"\a25\"\001\241The vertical joint between the two adjacent masonry wythes at\nthe ... do not appear to be bonded or tied together. Cracking of the\nmortar is resulting. \t\t\t\t\t\t\n\"\b728\"\001\242The mantle above factory built fireplace in the family room has\nbeen installed in an un-workmanlike manner. Gaps between the mantle and\nwall are too excessive. "], ["hello world"]] -- Posted via http://www.ruby-forum.com/.