serialization - Ruby Marshal.dump gives different results for what looks like the same thing -


i'm seeing different results ruby's marshal.dump depending on if called .to_s on or typed in characters. i'm not clear on what's happening here:

»  marshal.dump(1.to_s) => "\x04\bi\"\x061\x06:\x06ef" »  marshal.dump('1') => "\x04\bi\"\x061\x06:\x06et" »  1.to_s == '1' => true 

so although appears 1.to_s == '1', don't dump out same thing, difference in last byte. ideas why happening , how can both things dump same byte sequence?

marshal.load("\x04\bi\"\x061\x06:\x06ef").encoding # => #<encoding:us-ascii>  marshal.load("\x04\bi\"\x061\x06:\x06et").encoding # => #<encoding:utf-8> 

by default, 1.to_s.encoding not same '1'.encoding. however, both strings in 7-bit ascii range, comparable, , '1' == 1.to_s able give result true, after internal magic. not same thing.

marshal.dump(1.to_s.force_encoding('utf-8')) # => "\x04\bi\"\x061\x06:\x06et" marshal.dump('1') # => "\x04\bi\"\x061\x06:\x06et" 

(assuming run on newer ruby, , don't mess source encoding.)


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -