I have the following code, and I just want to make it look cleaner, any suggestions? Basically, a team has many games, and I want to merge all the games and order them by their attribute game.game_date??
@games = Array.new
@teams.each {|team|
team_games = team.games
@games << team_games
}
@games = @games.flatten
How about:
…or maybe:
…this can be written as follows on recent Ruby versions (not sure exactly when this came in):
NB: Symbol#to_proc (the bits that look like
&:symbol) needs a recent version of Ruby (not quite sure of the required version).This construct is the same as passing a block like
{ |arg| arg.symbol }to the method.Eg:
map(&:games)is equivalent tomap { |team| team.games }NB the second:
collectandmapare synonyms, as areinjectandreduce.