Forum: Ruby Issues with pack/write on WinXP compared to Linux

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E92d09efe27bf50a87967c533fae247a?d=identicon&s=25 unknown (Guest)
on 2007-03-23 19:51
(Received via mailing list)
I'm sure that this has to do with CR/LF on DOS/Windows filesystems,
but I'm not sure how to fix:

File.open('test', 'w') do |file|
  file.write([10].pack('n'))  #=> 2 [bytes]
end

On Linux, the file comes out as having 2 bytes (as expected):

% ls -l test
-rw-rw-r--    1 jamesm   eng             2 Mar 23 11:38 test
% od -h test
0000000 0a00
0000002

On WinXP it has 3 bytes (BTW, write still returns 2 [bytes] in irb on
WinXP):

bash-2.05a$ ls -l test
-rw-r--r--    1 Administ None            3 Mar 23 11:40 test
bash-2.05a$ od -h test
0000000 0d00 000a
0000003

This does not happen when writing out another number (say 11).  Again,
it's probably a CR/LF deal, but I don't know how to remedy.  Thanks in
advance...
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2007-03-23 19:56
unknown wrote:
> I'm sure that this has to do with CR/LF on DOS/Windows filesystems,
> but I'm not sure how to fix:
>
> File.open('test', 'w') do |file|
>   file.write([10].pack('n'))  #=> 2 [bytes]
> end

File.open('test', 'wb') do |file|
E92d09efe27bf50a87967c533fae247a?d=identicon&s=25 unknown (Guest)
on 2007-03-23 20:11
(Received via mailing list)
On Mar 23, 11:56 am, Tim Hunter <rmag...@gmail.com> wrote:
> File.open('test', 'wb') do |file|

Excellent - I knew it was something simple.  This was my first binary
and DOS/Windows project in Ruby... Thanks!
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2007-03-23 22:32
(Received via mailing list)
On Sat, 24 Mar 2007 james.d.masters@gmail.com wrote:

> I'm sure that this has to do with CR/LF on DOS/Windows filesystems,
> but I'm not sure how to fix:
>
> File.open('test', 'w') do |file|
                      ^
                      ^
                      ^
                      wb

-a
This topic is locked and can not be replied to.