I am not sure how this is valid code:
class Library
def initialize(games)
@games = games
end
def add_game(game)
games << game
end
def games()
@games
end
end
games = ['WoW','SC2','D3']
lib = Library.new(games)
puts lib.games
lib.add_game('Titan')
puts lib.games
This will print:
WoW SC2 D3 Titan
I think he should print
WoW SC2 D3
The add_game method does not use an instance variable. Being new to Ruby, I don't understand how this works. It should not be:
def add_games(game)
@games << game
end
I am reading this from a tutorial, and I could not find anything about how <<<works specifically with instance variables. I thought: “<<was just overloaded when working with arrays to“ add to the array. ”Does this really do something with Singleton classes?
source
share