# Marshalled data in 1.9

**URL:** <https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625>\
**Category:** Ruby\
**Created:** [June 24, 2010, 1:01am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625 "2010-06-24T01:01:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 24, 2010, 1:01am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/1 "2010-06-24T01:01:26Z")

</div>

Is there the possibility this would fail in 1.9?

big = eval(File.read(“out\_inspect.small”))  
File.open(“out.marshal”, “w”) do |f|

f.write(Marshal.dump(big))

end  
Marshal.load(File.open(‘out.marshal’, ‘r’))

When I do this with large structures (on windows) I get messages like:

bad.rb:7:in `load’: dump format error for symbol(0x6c) (ArgumentError)

irb(main):001:0\> Encoding.default\_external  
=\> #Encoding:IBM437  
irb(main):002:0\> Encoding.default\_internal  
=\> nil

But I had assumed since I was reading and writing in the same mode it  
would work all right. Was I wrong?  
-r

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 24, 2010, 1:14am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/2 "2010-06-24T01:14:56Z")

</div>

On Jun 23, 2010, at 7:01 PM, Roger P. wrote:

> But I had assumed since I was reading and writing in the same mode it  
> would work all right. Was I wrong?  
> -r

You almost certainly want the ‘rb’ and ‘wb’ modes on Windows to read  
and write in binary, rather than text, mode.

-Rob

Rob B.  
[http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

> **[Launch Scout Custom Software Development & Software Consulting](https://launchscout.com/)**
>
> We solve digital problems through custom software development and design for the web—apps, mobile, desktop and devices.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 25, 2010, 6:55am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/3 "2010-06-25T06:55:39Z")

</div>

> > But I had assumed since I was reading and writing in the same mode it  
> > would work all right. Was I wrong?  
> > -r
> 
> You almost certainly want the ‘rb’ and ‘wb’ modes on Windows to read  
> and write in binary, rather than text, mode.

Hmm. The problem may occur when I read the file in–because I’m not  
reading it in Binary mode, I’m actually reading it in as ascii + some  
encoding (Encoding.default\_external), which is IBM437

I’m still not entirely sure why something like this _shouldn’t_ round  
trip appropriately though.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 25, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/4 "2010-06-25T16:14:29Z")

</div>

> > I’m still not entirely sure why something like this _shouldn’t_ round  
> > trip appropriately though.
> 
> If you’re under Windows, ruby/C will translate \r\n to \n on read and  
> vice versa on write, unless you open the file in binary mode.

Yes, but if I read and write both in ASCII mode, should it not be  
expected to round trip? I’m a bit confused…

-r

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 25, 2010, 9:57am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/5 "2010-06-25T09:57:08Z")

</div>

Roger P. wrote:

> > > But I had assumed since I was reading and writing in the same mode it  
> > > would work all right. Was I wrong?  
> > > -r
> > 
> > You almost certainly want the ‘rb’ and ‘wb’ modes on Windows to read  
> > and write in binary, rather than text, mode.
> 
> Hmm. The problem may occur when I read the file in–because I’m not  
> reading it in Binary mode, I’m actually reading it in as ascii + some  
> encoding (Encoding.default\_external), which is IBM437
> 
> I’m still not entirely sure why something like this _shouldn’t_ round  
> trip appropriately though.

If you’re under Windows, ruby/C will translate \r\n to \n on read and  
vice versa on write, unless you open the file in binary mode.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 25, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/6 "2010-06-25T22:06:25Z")

</div>

> irb(main):002:0\> File.open(“zz”, “w”) {|io| io.write  
> “foo\r\nbar\nbaz\n”}  
> =\> 16  
> irb(main):003:0\> File.read(“zz”)  
> =\> “foo\n\nbar\nbaz\n”
> 
> Notice the “\r\n” came back as “\n\n”.

This feels like a bug to me…

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 25, 2010, 9:53pm UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/7 "2010-06-25T21:53:58Z")

</div>

Roger P. wrote:

> Yes, but if I read and write both in ASCII mode, should it not be  
> expected to round trip? I’m a bit confused…

irb(main):001:0\> [RUBY\_VERSION, RUBY\_PLATFORM]  
=\> [“1.9.2”, “i386-mswin32\_100”]  
irb(main):002:0\> File.open(“zz”, “w”) {|io| io.write  
“foo\r\nbar\nbaz\n”}  
=\> 16  
irb(main):003:0\> File.read(“zz”)  
=\> “foo\n\nbar\nbaz\n”

Notice the “\r\n” came back as “\n\n”.

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 26, 2010, 1:48am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/8 "2010-06-26T01:48:12Z")

</div>

Roger P. wrote:

> > irb(main):002:0\> File.open(“zz”, “w”) {|io| io.write  
> > “foo\r\nbar\nbaz\n”}  
> > =\> 16  
> > irb(main):003:0\> File.read(“zz”)  
> > =\> “foo\n\nbar\nbaz\n”
> > 
> > Notice the “\r\n” came back as “\n\n”.
> 
> This feels like a bug to me…

If I could pick one statement to summarize my feeling  
about developing on Windows, that might be it. 😜

But anyway…

It’s not strictly a ruby issue. I recall encountering CRLF  
text mode vs. binary mode issues in DOS with Borland C  
in the mid 1980’s.

But, back to the ruby example above… I don’t see  
how one could expect the data to survive a round trip.

Note that if we force ruby to deal with a single  
character at a time, the results are consistent with  
the above:

irb(main):004:0\> File.open(“zz2”, “w”) {|io|  
“foo\r\nbar\nbaz\n”.each\_char {|c| io.write c; io.flush}}  
=\> “foo\r\nbar\nbaz\n”

irb(main):005:0\> File.open(“zz2”, “r”) {|io| x=c=""; x \<\< c while (c =  
io.getc); x}  
=\> “foo\n\nbar\nbaz\n”

So… it’s not clear to me how round-trip semantics  
could be implemented given the need to consider each  
character individually?

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 26, 2010, 2:40am UTC](https://www.ruby-forum.com/t/marshalled-data-in-1-9/190625/9 "2010-06-26T02:40:53Z")

</div>

On Jun 25, 7:44 pm, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> how one could expect the data to survive a round trip.
> 
> So… it’s not clear to me how round-trip semantics  
> could be implemented given the need to consider each  
> character individually?

If you want to specify the new lines yourself, you need to use binary  
mode.

Text mode read and write under Windows will do weird things, but is  
defined as the “spec” of Ruby behavior.
