此方法只是更改找到的任何player_id的id。 Reek討厭它,但我找不到一種有意義的方式來重構它。使用克隆時,如何擺脫ruby中的重複?
(1..9).each { |n|
n = n.to_s
self.player_ids[n] = self.site_id.clone << "_" << self.player_ids[n].clone if self.player_ids[n]
}
難道我只是有這種重複的生活,因爲克隆功能不允許我:
player_id = self.player_ids[n]
player_id = self.site_id.clone << "_" << player_id.clone if player_id
樣品輸入:
{:player_ids => {"2" => "player_name1", "6" => "player_name4", "9" => "player_name9"}
輸出:
{:player_ids => {"2" => "PRE_player_name1", "6" => "PRE_player_name4", "9" => "PRE_player_name9"}
你可以試試'self.site_id +「_」+ self.player_ids [n] if self.player_ids [n]'? – Dogbert 2011-04-05 13:59:35
這個克隆和就地東西看起來很糟糕。你確定你想要嗎?爲什麼不向我們展示一個(或更多)pair輸入/ desired_output,以便我們能夠弄清楚你想要什麼。 – tokland 2011-04-05 14:30:30
對不起,我應該養成顯示輸入和輸出的習慣。 – 2011-04-05 14:42:18