Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Bitwise-IO/Ruby/bitwise-io.rb
Normal file
35
Task/Bitwise-IO/Ruby/bitwise-io.rb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
def crunch(ascii)
|
||||
bitstring = ascii.bytes.collect {|b| "%07b" % b}.join
|
||||
[bitstring].pack("B*")
|
||||
end
|
||||
|
||||
def expand(binary)
|
||||
bitstring = binary.unpack("B*")[0]
|
||||
bitstring.scan(/[01]{7}/).collect {|b| b.to_i(2).chr}.join
|
||||
end
|
||||
|
||||
original = "This is an ascii string that will be crunched, written, read and expanded."
|
||||
puts "my ascii string is #{original.length} bytes"
|
||||
|
||||
filename = "crunched.out"
|
||||
|
||||
# write the compressed data
|
||||
File.open(filename, "w") do |fh|
|
||||
fh.binmode
|
||||
fh.print crunch(original)
|
||||
end
|
||||
|
||||
filesize = File.size(filename)
|
||||
puts "the file containing the crunched text is #{filesize} bytes"
|
||||
|
||||
# read and expand
|
||||
expanded = File.open(filename, "r") do |fh|
|
||||
fh.binmode
|
||||
expand(fh.read)
|
||||
end
|
||||
|
||||
if original == expanded
|
||||
puts "success"
|
||||
else
|
||||
puts "fail!"
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue