From 00ce65ab8bc23bc8926e7856a6a134d92c1af853 Mon Sep 17 00:00:00 2001 From: popmac Date: Wed, 6 Dec 2017 15:07:32 +0900 Subject: [PATCH] =?UTF-8?q?transport=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=82=92=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2行目では、splitメソッドの引数を省略して、mapメソッドで&:のイディオムを使用。 3行目では、行と列の入れ替えのためにArray#transposeを使用。 --- lib/transport.rb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/transport.rb b/lib/transport.rb index d930638..e686c07 100644 --- a/lib/transport.rb +++ b/lib/transport.rb @@ -1,11 +1,4 @@ def transport(source) - array = source.split("\n").map {|s| s.split(" ")} - rows_count = array.first.count - - transported_array = [] - 0.upto(rows_count - 1) do |i| - transported_array << array.map {|a| a[i]} - end - - transported_array.map {|s| s.join(" ")}.join("\n") -end \ No newline at end of file + array = source.split("\n").map(&:split) + array.transpose.map {|s| s.join(" ")}.join("\n") +end