Created
April 25, 2015 22:26
-
-
Save JunichiIto/d4aa039f4b8aad040453 to your computer and use it in GitHub Desktop.
Refactoring http://ja.stackoverflow.com/a/9556/85
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
require 'csv' | |
class FillBlankTest < Test::Unit::TestCase | |
def fill_blank(csv_text) | |
CSV.parse(csv_text).inject([]) { |results, row| | |
results << row.zip(results.last || []).map { |curr, prev| curr || prev } | |
} | |
.map { |row| row.join(',') } | |
.join("\n") | |
end | |
def test_fill_blank | |
input = <<-TEXT | |
日本,東京,新宿 | |
,,渋谷 | |
,兵庫,神戸 | |
アメリカ,カリフォルニア,サンフランシスコ | |
,,ロサンゼルス | |
TEXT | |
output = <<-TEXT.chomp | |
日本,東京,新宿 | |
日本,東京,渋谷 | |
日本,兵庫,神戸 | |
アメリカ,カリフォルニア,サンフランシスコ | |
アメリカ,カリフォルニア,ロサンゼルス | |
TEXT | |
assert_equal output, fill_blank(input) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment