From codeschool's ruby-bits course, I am trying to understand how these classes work- I have a Game
class and a collection class called Library
that stores a collection of games.
class Game
attr_accessor :name, :year, :system
attr_reader :created_at
def initialize(name, options={})
self.name = name
self.year = options[:year]
self.system = options[:system]
@created_at = Time.now
end
def ==(game)
name == game.name &&
system == game.system &&
year == game.year
end
end
Library class:
class Library
attr_accessor :games
def initialize(*games)
self.games = games
end
def has_game?(*games)
for game in self.games
return true if game == game
end
false
end
end
Now I create some games:
contra = Game.new('Contra', {
year: 1994,
system: 'nintendo'
})
mario = Game.new('Mario', {
year: 1996,
system: 'SNES'
})
sonic = Game.new('Sonic', {
year: 1993,
system: 'SEGA'
})
and instantiate a new collection:
myCollection = Library.new(mario, sonic)
When I try to find if a certain game is in myCollection
using the has_game?
method, I always get true
puts myCollection.has_game?(contra) #=> returns **true**
even though this has never been inserted as part of the collection.
What am I doing wrong?