# Ruby way of dumping file

**URL:** <https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814>\
**Category:** Ruby\
**Created:** [December 17, 2008, 6:19am UTC](https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814 "2008-12-17T06:19:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 6:19am UTC](https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814/1 "2008-12-17T06:19:28Z")

</div>

Hi,  
Can someone give me a cleaner, more rubyesque (even correct) way of  
printing  
out the first 20 bytes of a file? My solution doesn’t feel right.

open(ARGV[0], ‘rb’) { |f|  
(0…20).each do |n|  
printf "%02d: ", n  
f.read(1).each\_byte { | b | printf “0x%02x \n”, b }  
end  
}

Thanks.

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 6:50am UTC](https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814/2 "2008-12-17T06:50:18Z")

</div>

Joshua B. wrote:

> Hi,  
> Can someone give me a cleaner, more rubyesque (even correct) way of  
> printing  
> out the first 20 bytes of a file? My solution doesn’t feel right.

print File.read(ARGV.first)[0…20]

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 7:10am UTC](https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814/3 "2008-12-17T07:10:19Z")

</div>

On 17.12.2008, at 05:42 , The Higgs bozo wrote:

> Joshua B. wrote:
> 
> > Hi,  
> > Can someone give me a cleaner, more rubyesque (even correct) way of  
> > printing  
> > out the first 20 bytes of a file? My solution doesn’t feel right.
> 
> ## print File.read(ARGV.first)[0…20]
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Doesn’t that read the entire file before extracting the first 20  
bytes? Seems silly to me.

rather something like this:

open ARGV.first, “rb” do |f|  
read = “”  
begin  
read += f.read(20)  
end while read.size \< 20 && !f.eof?  
p read[0…20]  
end

although I suppose this would work too:

p open(ARGV[0], “rb”){|f|f.read(20)}

but I don’t think read(20) is guaranteed to read 20 bytes but maybe  
that is just in sockets.

einarmagnus

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 7:34am UTC](https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814/4 "2008-12-17T07:34:18Z")

</div>

From: Joshua B. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Can someone give me a cleaner, more rubyesque (even correct)

# way of printing out the first 20 bytes of a file? My solution

# doesn’t feel right.

# 

# open(ARGV[0], ‘rb’) { |f| …

try,

IO.read ARGV[0], 20

---

<div class="post-metadata">

**Author:** ![jballanc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/97f17d/32.png) [@jballanc](https://www.ruby-forum.com/u/jballanc)\
**Post date:** [December 17, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/ruby-way-of-dumping-file/155814/5 "2008-12-17T12:43:30Z")

</div>

Einar MagnÃºs Boson wrote:

> p open(ARGV[0], “rb”){|f|f.read(20)}
> 
> but I don’t think read(20) is guaranteed to read 20 bytes but maybe  
> that is just in sockets.

In Ruby, I believe it’s guaranteed to read 20 bytes, from both files and  
sockets - it will restart the underlying OS calls if necessary - unless  
EOF is reached.

I think the each\_byte approach is cleanest, but here are some  
alternatives:

# not ruby-1.9 clean

puts File.read(ARGV[0],20).gsub(/(.)/) { “0x%02x\n” % $1[0] }

# different output format

puts File.read(ARGV[0],20).unpack(“H\*”)[0].scan(/…/).join(" ")  
puts File.read(ARGV[0],20).unpack("H2 “\*20).join(” ")
