Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor transport method by @cheezenaan #22

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions lib/transport.rb
@@ -1,11 +1,5 @@
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
def transpose(source)
source_array = source.split("\n").map(&:split)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[IMO]
&演算子を使うと、ブロックを使った処理をもう少しシュッと書けるようになります😄
ref. https://qiita.com/kasei-san/items/0392097791d3a5998216

# これら2つの結果は同じです :D
irb(main):018:0> (1..10).map { |n| n.to_s }
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
irb(main):017:0> (1..10).map(&:to_s)
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

transposed_array = source_array.transpose
Copy link
Author

@cheezenaan cheezenaan Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[IMO]
自分がやりたいと思っていることは、たいてい他の誰かもやりたいと思っています。
その実現方法がたとえば gem として公開されていたり、 Ruby 標準のライブラリとして実装されていることが多いです。

今回タケシさんがやりたいことは Array#transpose として実装されているので、こちらを使ってみましょう。
ref. https://ref.xaio.jp/ruby/classes/array/transpose

transposed_array.map { |nums| nums.join(" ") }.join("\n")
end
42 changes: 32 additions & 10 deletions test/transport_test.rb
@@ -1,14 +1,36 @@
require 'minitest/autorun'
require './lib/transport'

class TransportTest < MiniTest::Test
def test_transport
input = "1 2 3\n4 5 6\n7 8 9"
output = "1 4 7\n2 5 8\n3 6 9"
assert_equal output, transport(input)

input = "1 2 3\n4 5 6\n7 8 9\n10 11 12"
output = "1 4 7 10\n2 5 8 11\n3 6 9 12"
assert_equal output, transport(input)
class Transpose < MiniTest::Test
def test_transpose
input = <<~EOS
Copy link
Author

@cheezenaan cheezenaan Dec 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[IMO]
「ドキュメントとしてのテストコード」という観点をもって書いてみると「より読みやすくできるのではないか…:thinking:」と考えを深められるかもしれません。

[IMO]
Ruby 2.4(2.3?) 系からはヒアドキュメントに <~~EOS が追加されたので、これを使ってみるといいと思います。
ref. https://docs.ruby-lang.org/ja/latest/doc/spec=2fliteral.html#here

# たとえば、こんなかんじです
text = <~~EOS
  劇場版 響け!ユーフォニアム
  〜北宇治高校吹奏楽部へようこそ〜
EOS

1 2 3
4 5 6
7 8 9
EOS

output = <<~EOS
1 4 7
2 5 8
3 6 9
EOS

# <~~ 記法だと末尾に改行文字(\n)が入るため #String#chomp で取り除く
assert_equal output.chomp, transpose(input)

input = <<~EOS
1 2 3
4 5 6
7 8 9
10 11 12
EOS

output = <<~EOS
1 4 7 10
2 5 8 11
3 6 9 12
EOS

assert_equal output.chomp, transpose(input)
end
end
end