# File 'rb' on non-Windows

**URL:** <https://www.ruby-forum.com/t/file-rb-on-non-windows/119723>\
**Category:** Ruby\
**Created:** [November 9, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/file-rb-on-non-windows/119723 "2007-11-09T16:18:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 9, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/file-rb-on-non-windows/119723/1 "2007-11-09T16:18:33Z")

</div>

To open binary files, on Windows the statement:  
File.new(“myfile”, “rb”)  
is required.

The docs just seem to say that the “b” is a Windows-only thing, not what  
happens on other platforms. Will the “b” flag just be filtered out on  
Linux/OS X?

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 9, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/file-rb-on-non-windows/119723/2 "2007-11-09T16:57:16Z")

</div>

Jari W. wrote:

> To open binary files, on Windows the statement:  
> File.new(“myfile”, “rb”)  
> is required.
> 
> The docs just seem to say that the “b” is a Windows-only thing, not what  
> happens on other platforms. Will the “b” flag just be filtered out on  
> Linux/OS X?
> 
> Best regards,
> 
> Jari W.

On Windows files will usually have line breaks build by the sequence  
“\r\n”. When reading such a file in normal mode, the “\r” will be  
deleted and not inside the String. When writing data to a file, each  
“\n” will be replaced by “\r\n”.

When using “rb” or “wb” on Windows, the data will be unchanged.

On Linux/OS X the line break character in a file is “\n”, so the “b”  
mode doesn’t have any effect.

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 9, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/file-rb-on-non-windows/119723/3 "2007-11-09T17:00:43Z")

</div>

Jari W. wrote:

> To open binary files, on Windows the statement:  
> File.new(“myfile”, “rb”)  
> is required.
> 
> The docs just seem to say that the “b” is a Windows-only thing, not what  
> happens on other platforms. Will the “b” flag just be filtered out on  
> Linux/OS X?

It is ignored on other platforms, yes.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 9, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/file-rb-on-non-windows/119723/4 "2007-11-09T19:22:25Z")

</div>

Jari W. wrote:

> To open binary files, on Windows the statement:  
> File.new(“myfile”, “rb”)  
> is required.
> 
> The docs just seem to say that the “b” is a Windows-only thing, not what  
> happens on other platforms. Will the “b” flag just be filtered out on  
> Linux/OS X?

1. Different os’s use different characters for newlines.

2. In Ruby, and other languages, when you read or write a file, ruby  
automatically converts all newlines from any os to: ‘\n’. That way,  
when you write a file, you can have your code write a ‘\n’ to the file  
and ruby will take care of converting the ‘\n’ to the proper newline for  
your system. Likewise, when you read a newline from a file, you can  
assume the newline is a ‘\n’ because ruby will convert the actual  
newline to a ‘\n’. In other words, you can write code that assumes ‘\n’  
is the newline on any os–even though the actual newline for that os may  
be something else.

3. When you open a file in binary mode, you are directing ruby _not_ to  
do any newline conversions. That means you can no longer assume the  
newlines are ‘\n’. The newlines you read will be the actual newlines  
that are in the file. Similarly, you can no longer write a ‘\n’ to the  
file and expect ruby to convert it to the proper newline for the os. In  
other words, what you read from a file is exactly what’s in the file,  
and what you write to the file is exactly what’s in your write or puts  
statement.

4. Because newlines on unix are a ‘\n’, ruby doesn’t have to do any  
conversions when you read or write a text file in normal mode.  
Therefore, if you open a file in binary mode on a unix os, which tells  
ruby not to do any conversions, there is no difference. What you read  
from a file is exactly what’s in the file, i.e. the newline in the file  
and what ruby returns to you as the newline are identical, and when you  
write a ‘\n’ to the file, an actual ‘\n’ gets written to the file.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 9, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/file-rb-on-non-windows/119723/5 "2007-11-09T17:14:45Z")

</div>

On Nov 9, 2007, at 10:59 AM, Daniel B. wrote:

> Regards,
> 
> Dan

It’s not “ignored”, it just has no effect because there’s no  
difference between “binary” mode and the default for Unix-like systems.

-Rob

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