+ 1

Instance variable functions

1. What is the difference between including @cards as an argument in the initialize method vs. not including it as an argument? 2. For the class Player below does not including @cards as an argument in the initialize method mean that all instances of the class (in other words each player) will have the same number of cards? 3. Can I set each player to have a different number of cards later without modifying the code below? class Player def initialize (name) @name = name @cards = [] end end player1 = Player.new("Kuba")

11th Nov 2018, 5:06 PM
Ana Lopez
Ana Lopez - avatar
1 Answer
+ 5
Having an argument allows each instance to start with a different set of array elements. However, you can add elements to the array later without issue. Your code currently makes the array with no elements. You can: def add(element) @cards.push(element) end player1.add('5s')
28th Nov 2018, 10:19 PM
John Wells
John Wells - avatar