【发布时间】:2014-06-10 17:25:26
【问题描述】:
@baseball_games = 0
@basketball_games = 0
@football_games = 0
Game.all.each do |game|
instance_variable_set("@#{game.sport.name.downcase}_games", instance_variable_get("@#{game.sport.name.downcase}_games") + 1)
end
有没有比在 set 方法中调用 get 方法更好的方法呢?我真的只是想+=动态变量...
【问题讨论】:
-
闻起来像糟糕的设计。实现
[]=怎么样? -
对,把它们存储在一个哈希中
-
啊啊啊啊啊。
games = {basketball: 0, football: 0, baseball: 0},然后是games[game.sport.name.downcase] += 1