Question

This is a weird issue. I have a mashalled object(cookie), an array with 2 objects which has been base64 encoded. I decode this using decode64 and marshal.load it and get the array back fine.

Now I take this array and marshal.dump it and compare it to the original representation. The 2 encodings dont match. EF at the end of the string vs ET on the second.

Strangely enough they match if I use irb.

Same version of ruby. What am I missing?

#!/usr/bin/env ruby -v  

require "base64"  
require "cgi"  

cookie = "BAhbB2kHSSJFNThhYmY3ZjRiOWY0OTc4NjMxOTNhNTllMzQ1YjYxNTVlMGE2NTIzZDNjZmZmZDYxNWQwNTVhNmJkMzI0ZWIxYQY6BkVU"  
p Marshal.load(Base64.decode64(cookie))  
p Base64.decode64(cookie)  
p Marshal.dump([2, "58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a"])  

Output:

ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.5.0]  
[2, "58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a"]  
"\x04\b[\ai\aI\"E58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a\x06:\x06ET"  
"\x04\b[\ai\aI\"E58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a\x06:\x06EF"

irb output:

ruby-1.9.2-p136 :001 > p Marshal.dump([2, "58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a"])  
"\x04\b[\ai\aI\"E58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a\x06:\x06ET"  
 => "\x04\b[\ai\aI\"E58abf7f4b9f497863193a59e345b6155e0a6523d3cfffd615d055a6bd324eb1a\x06:\x06ET"  
Was it helpful?

Solution

As rue on ruby-lang on freenode pointed out, there was a difference in the encoding in irb vs script.

foo.encoding in script -> #<Encoding:US-ASCII>
foo.encoding in irb -> #<Encoding:UTF-8>

adding the following magic comment to script resolves the issue

#encoding: utf-8

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top