# Loading a file in a buffer

**URL:** <https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825>\
**Category:** Ruby\
**Created:** [March 3, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825 "2008-03-03T18:20:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 3, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825/1 "2008-03-03T18:20:27Z")

</div>

Hello everybody,

I would like to load the exact content of a file in a buffer (with the  
same carriage return). To do so, I had trying this:

file = File.open(’/home/my\_file’)

To verify if that work normally, I had try to add this for display the  
content of my\_file:

puts file

But my\_file was not been printed, and I got: #\<File:0x28d70\>

If you know a way to do so, please help me 🙂  
Thanks.

Zang’

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 3, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825/2 "2008-03-03T18:32:41Z")

</div>

Alle Monday 03 March 2008, Zangief I. ha scritto:

> puts file
> 
> But my\_file was not been printed, and I got: #\<File:0x28d70\>
> 
> If you know a way to do so, please help me 🙂  
> Thanks.
> 
> Zang’

To get the contents of a file, opening it is not enough. You need to  
explicitly read its contents. There are many methods which allow to  
access the  
contents of a file: read, readlines, each\_line, each\_byte, File.read,  
File.readlines, File.foreach (the last three are class methods). They’re  
documented under class IO (from which File is derived). If you only need  
to  
read the contents of the file you don’t need to use File.open at all,  
but  
simply use File.read:

contents = File.read(’/home/my\_file’/)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 3, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825/3 "2008-03-03T18:35:38Z")

</div>

On Mon, Mar 3, 2008 at 12:20 PM, Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts file
> 
> But my\_file was not been printed, and I got: #\<File:0x28d70\>

try:

File.open(“/home/my\_file”, “r”) do |infile|  
while (line = infile.gets)  
puts “#{counter}: #{line}”  
counter = counter + 1  
end  
end

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 3, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825/4 "2008-03-03T18:38:36Z")

</div>

On Mar 3, 2008, at 12:20 PM, Zangief I. wrote:

> puts file
> 
> But my\_file was not been printed, and I got: #\<File:0x28d70\>
> 
> If you know a way to do so, please help me 🙂

Try

```
buffer = IO.read('/home/my_file')

```

or

```
buffer = File.read('/home/my_file')

```

They both do the same thing because File inherits ‘read’ from IO.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [March 3, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/loading-a-file-in-a-buffer/130825/5 "2008-03-03T18:52:48Z")

</div>

Thanks! ‘read’ method works very well 🙂
