mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
backporting IO#binread for 1.8 users
This commit is contained in:
parent
0b212117e6
commit
681bf1fc48
2 changed files with 38 additions and 0 deletions
15
activesupport/lib/active_support/core_ext/io.rb
Normal file
15
activesupport/lib/active_support/core_ext/io.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
if RUBY_VERSION < '1.9.2'
|
||||
|
||||
# :stopdoc:
|
||||
class IO
|
||||
def self.binread(name, length = nil, offset = nil)
|
||||
return File.read name unless length || offset
|
||||
File.open(name, 'rb') { |f|
|
||||
f.seek offset if offset
|
||||
f.read length
|
||||
}
|
||||
end
|
||||
end
|
||||
# :startdoc:
|
||||
|
||||
end
|
23
activesupport/test/core_ext/io_test.rb
Normal file
23
activesupport/test/core_ext/io_test.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require 'abstract_unit'
|
||||
|
||||
require 'active_support/core_ext/io'
|
||||
|
||||
class IOTest < Test::Unit::TestCase
|
||||
def test_binread_one_arg
|
||||
assert_equal File.read(__FILE__), IO.binread(__FILE__)
|
||||
end
|
||||
|
||||
def test_binread_two_args
|
||||
assert_equal File.read(__FILE__).bytes.first(10).pack('C*'),
|
||||
IO.binread(__FILE__, 10)
|
||||
end
|
||||
|
||||
def test_binread_three_args
|
||||
actual = IO.binread(__FILE__, 5, 10)
|
||||
expected = File.open(__FILE__, 'rb') { |f|
|
||||
f.seek 10
|
||||
f.read 5
|
||||
}
|
||||
assert_equal expected, actual
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue