ruby - Object containing an array going to nil -


i have class deck represents array of 81 cards when try use getter functions on card in array gives error undefined method getcolor nil:nilclass (nomethoderror) assume means in array equals nil.

i trying figure out how set deck class can return card using getcard function , use card getter methods obtain values of individual card.

class card       def initialize id, symbol, color, shading, number         @cardid = id #0-80 unique each indivdual card in deck         @cardsymbol = symbol # 0 = diamond 1 = squigly 2 = oval         @cardcolor = color # 0 = green 1 = blue 2 = pink         @cardshading = shading # 0 = none 1 = lines 2 = solid         @cardnumber = number #number of symbols on card     end      def getid         @cardid     end      def getsymbol         @cardsymbol     end      def getcolor         @cardcolor     end      def getshading         @cardshading     end      def getnumber         @cardnumber     end  end  class deck      def initialize         @deck = array.new(81)         @carddealt = array.new(81)     end      def addcardstodeck           cardid = 0         symbol = 0         color = 0         shading = 0         number = 0         while cardid < 80             if symbol > 2                 symbol = 0                 color = color + 1             elsif color > 2                 color = 0                 shading = shading + 1             elsif shading > 2                 shading = 0                 number = number + 1             else                 symbol = symbol + 1             end              card = card.new(cardid, symbol, color, shading, number)             cardid = cardid + 1             @deck[cardid] = @card         end      end      def getcard cardid         @deck[cardid]     end end  setdeck = deck.new setdeck.addcardstodeck card0 = setdeck.getcard(0) puts "color #{card0.getcolor}" 

the lines

card = card.new(cardid, symbol, color, shading, number) cardid = cardid + 1 @deck[cardid] = @card 

are problem. note how local variable card set in first line, while uninitialized instance variable @card used in third. sets entries nil.

another issue incrementing cardid before using index. set card 0 slot 1 etc. classic "one off" error.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -