5
私はチェッカープログラムをリファクタリングしていますが、プレーヤーの移動要求を(例えば "3、3、5、5"のような形で)intに処理しようとしていますアレイ。私は、次のような方法がありますが、私はそれができる知っているとして、それは、Ruby-など感じない:リファクタリングRuby:文字配列をint配列に変換する
def translate_move_request_to_coordinates(move_request)
return_array = []
coords_array = move_request.chomp.split(',')
coords_array.each_with_index do |i, x|
return_array[x] = i.to_i
end
return_array
end
私はそれで、次のRSpecのテストを持っています。
it "translates a move request string into an array of coordinates" do
player_input = "3, 3, 5, 5"
translated_array = @game.translate_move_request_to_coordinates(player_input)
translated_array.should == [3, 3, 5, 5]
end
テストに合格しますが、コードはかなり醜いと思います。どんな助けもありがとう。ありがとう。
スティーブ
move_request.split( "")マップ(&:to_i)を見てください。 – tokland
+1を:私はそれを知りませんでした。 「Pragmatic Bookshelf」(http://pragprog.com/)の「Programming Ruby 1.9」の「Symbol.to_procトリック」のセクション(第4刷、2011年5月)の興味深い説明があります。 – lkuty