我有一个Team
类:如何从数组中将多个对象铲入对象?
class Team
attr_accessor :teamplayers
def initialize
@team_players = []
end
def <<(player)
@team_players << player
end
def to_s
puts "THIS IS THE TEAM #{@team_players}"
end
end
我想成员添加到团队<<
。我使用此代码:
team << @goalkeepers.last
team << @defenders[-1..-4]
team << @midfielders[-1]
team << @attackers[-1..-2]
第一行正常工作,并向团队中添加一个成员。然而,其他线路将阵列添加到我的团队,而不是实际的成员。
如何分别添加成员?
问题是什么?你是否得到一个错误,因为你的代码似乎会做你的标题所暗示的。 – 2013-02-28 11:39:40