Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Ruby]ファイルの内容をArrayに書き出すモジュール
#! ruby
#-*- encoding: utf-8 -*-
module FileReader
DELETE_LINEFEED_CODE = true
KEEP_LINEFEED_CODE = !DELETE_LINEFEED_CODE
def read(path, delete_linefeed_code = true)
content = Array.new
File.open(path, "r") do |file|
file.each do |line|
content.push(line) unless delete_linefeed_code
content.push(line.chomp) if delete_linefeed_code
end
end
return content
end
end
if __FILE__ == $0
require 'test/unit'
require './array-to-tempfile'
class FileReaderTest < Test::Unit::TestCase
include FileReader
include ArrayToTempfile
def test_read
# setup
expected = ["line1\n", "line2\n", "line3\n"]
write_array_to_tempfile(expected)
# exercise
actual = read(get_tempfile_path, FileReader::KEEP_LINEFEED_CODE)
# verify
assert_equal(3, actual.size)
assert_equal(expected, actual)
expected.each_with_index do |element, index|
assert_equal(expected[index], actual[index])
end
# teardown
delete_tempfile
end
def test_read_delete_linefeed_code
#setup
content = ["line1\n", "line2\n", "line3\n"]
write_array_to_tempfile(content)
expected = ["line1", "line2", "line3"]
# exercise
actual = read(get_tempfile_path)
# verify
assert_equal(3, actual.size)
assert_equal(expected, actual)
expected.each_with_index do |element, index|
assert_equal(expected[index], actual[index])
end
# teardown
delete_tempfile
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.