[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby way of dumping file

Joshua Ball

12/17/2008 5:11:00 AM

[Note: parts of this message were removed to make it a legal post.]

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.

5 Answers

The Higgs bozo

12/17/2008 5:43:00 AM

0

Joshua Ball 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-....

Einar Magnús Boson

12/17/2008 6:02:00 AM

0


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

> Joshua Ball 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-....
>


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




Peña, Botp

12/17/2008 6:26:00 AM

0

From: Joshua Ball [mailto:chezball@gmail.com]=20
# Can someone give me a cleaner, more rubyesque (even correct)=20
# way of printing out the first 20 bytes of a file? My solution
# doesn't feel right.
#=20
# open(ARGV[0], 'rb') { |f| ...


try,

IO.read ARGV[0], 20



Brian Candler

12/17/2008 11:36:00 AM

0

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(" ")
--
Posted via http://www.ruby-....

Mulligan

5/1/2013 12:11:00 PM

0

On May 1, 7:58 am, A Really Really Large Number <t2judgm...@gmail.com>
wrote:

> Under Bashar, Syrian
> corruption in Lebanon, which was already estimated at $2 billion per
> year in the 1990s,[24] became more rampant and was publicly exposed
> with the collapse in 2003 of the Lebanese Al-Madina bank.[25] Al-
> Madina was used to launder kickback money in the illegal gaming of the
> UN's Iraqi oil-for-food programme. Sources put the amount transferred
> and laundered through al-Madina at more than $1 billion, with a 25
> percent commission going to Syrian officials and their Lebanese
> allies, among the recipients of this money were Bashar Assad's brother
> Maher, Emile Lahoud's son-in-law Elias Murr, and Ghazali

This is called evidence.