【发布时间】:2011-12-15 02:19:01
【问题描述】:
我正在重构一个跳棋程序,并且我正在尝试将玩家移动请求(例如以“3、3、5、5”的形式)处理成一个 int 数组。我有以下方法,但感觉不像我知道的那样像 Ruby:
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
测试通过了,但我认为代码很丑。任何帮助,将不胜感激。谢谢。
史蒂夫
【问题讨论】: