Ruby program returns error “implicit conversion of String into Integer (TypeError)”












0















I'm trying to create a replica of Go Fish to help me learn more about arrays and hashes, and just how to go about structuring data. I'm on day two and have what looks to be much closer to the end goal. Keep in mind, I'm new to this. Anyway, here's the problem I'm running into:




=> gofish.rb:21:in `player_turn': no implicit conversion of String into Integer (TypeError)




I understand why I'm getting the error, but I can't figure out how to use the .shift method without giving an index number. I would like to select which object to shift based on the value instead. So, if I correctly guess do you have an 'ace of spades', the card is removed from the cpu_hand array and is added to the my_hand array. With that said, I would just like to know the best way to go about this.



Here's my script:



card_values = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'diamonds', 'hearts', 'clubs']

# creates array objects with every value and suit possible (full deck)
card_deck = card_values.product(suits).collect{|card, suit| "#{card} of #{suit}"}

def print_hand
puts "Your hand: #{@my_hand.join(', ')}."
end

def player_turn
puts "You go first!"
puts "Do you have a..."
puts @cpu_hand.join(', ')
print "> "
@card = $stdin.gets.chomp.downcase
# if cpu has the card requested give it to the player and add to their array
if @cpu_hand.include?(@card)
puts "Ahhh...you got me. Here you go!"
@my_hand.shift(@cpu_hand[@card]) # ****Here's the error(line:21)
print_hand
else
puts "Go fish!"
@my_hand.shift(@card_deck[1])
print_hand
end
end


puts "There are #{card_deck.length} cards in this deck."
puts "Welcome to Go-Fish."
print "Your name please: "
player_name = $stdin.gets.chomp.capitalize

puts "Ok #{player_name}, lets get this deck shuffled..."
#sleep(1)
# shuffles card_deck using .shuffle method
card_deck = card_deck.shuffle
puts "Cards are perfectly shuffled!"
#sleep(1)
puts "Dealing cards..."
#sleep(1)
# assigns first 7 cards to user, removes from card_deck
@my_hand = Array.new
@my_hand = card_deck.shift(7)
# assigns next 7 cards to CPU, removes from card_deck
@cpu_hand = Array.new
@cpu_hand = card_deck.shift(7)

print_hand

until card_deck.length < 1 || @cpu_hand.length < 1 || @my_hand.length < 1
player_turn
end

puts "GAME OVER!"









share|improve this question





























    0















    I'm trying to create a replica of Go Fish to help me learn more about arrays and hashes, and just how to go about structuring data. I'm on day two and have what looks to be much closer to the end goal. Keep in mind, I'm new to this. Anyway, here's the problem I'm running into:




    => gofish.rb:21:in `player_turn': no implicit conversion of String into Integer (TypeError)




    I understand why I'm getting the error, but I can't figure out how to use the .shift method without giving an index number. I would like to select which object to shift based on the value instead. So, if I correctly guess do you have an 'ace of spades', the card is removed from the cpu_hand array and is added to the my_hand array. With that said, I would just like to know the best way to go about this.



    Here's my script:



    card_values = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
    'eight', 'nine', 'ten', 'jack', 'queen', 'king']
    suits = ['spades', 'diamonds', 'hearts', 'clubs']

    # creates array objects with every value and suit possible (full deck)
    card_deck = card_values.product(suits).collect{|card, suit| "#{card} of #{suit}"}

    def print_hand
    puts "Your hand: #{@my_hand.join(', ')}."
    end

    def player_turn
    puts "You go first!"
    puts "Do you have a..."
    puts @cpu_hand.join(', ')
    print "> "
    @card = $stdin.gets.chomp.downcase
    # if cpu has the card requested give it to the player and add to their array
    if @cpu_hand.include?(@card)
    puts "Ahhh...you got me. Here you go!"
    @my_hand.shift(@cpu_hand[@card]) # ****Here's the error(line:21)
    print_hand
    else
    puts "Go fish!"
    @my_hand.shift(@card_deck[1])
    print_hand
    end
    end


    puts "There are #{card_deck.length} cards in this deck."
    puts "Welcome to Go-Fish."
    print "Your name please: "
    player_name = $stdin.gets.chomp.capitalize

    puts "Ok #{player_name}, lets get this deck shuffled..."
    #sleep(1)
    # shuffles card_deck using .shuffle method
    card_deck = card_deck.shuffle
    puts "Cards are perfectly shuffled!"
    #sleep(1)
    puts "Dealing cards..."
    #sleep(1)
    # assigns first 7 cards to user, removes from card_deck
    @my_hand = Array.new
    @my_hand = card_deck.shift(7)
    # assigns next 7 cards to CPU, removes from card_deck
    @cpu_hand = Array.new
    @cpu_hand = card_deck.shift(7)

    print_hand

    until card_deck.length < 1 || @cpu_hand.length < 1 || @my_hand.length < 1
    player_turn
    end

    puts "GAME OVER!"









    share|improve this question



























      0












      0








      0








      I'm trying to create a replica of Go Fish to help me learn more about arrays and hashes, and just how to go about structuring data. I'm on day two and have what looks to be much closer to the end goal. Keep in mind, I'm new to this. Anyway, here's the problem I'm running into:




      => gofish.rb:21:in `player_turn': no implicit conversion of String into Integer (TypeError)




      I understand why I'm getting the error, but I can't figure out how to use the .shift method without giving an index number. I would like to select which object to shift based on the value instead. So, if I correctly guess do you have an 'ace of spades', the card is removed from the cpu_hand array and is added to the my_hand array. With that said, I would just like to know the best way to go about this.



      Here's my script:



      card_values = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
      'eight', 'nine', 'ten', 'jack', 'queen', 'king']
      suits = ['spades', 'diamonds', 'hearts', 'clubs']

      # creates array objects with every value and suit possible (full deck)
      card_deck = card_values.product(suits).collect{|card, suit| "#{card} of #{suit}"}

      def print_hand
      puts "Your hand: #{@my_hand.join(', ')}."
      end

      def player_turn
      puts "You go first!"
      puts "Do you have a..."
      puts @cpu_hand.join(', ')
      print "> "
      @card = $stdin.gets.chomp.downcase
      # if cpu has the card requested give it to the player and add to their array
      if @cpu_hand.include?(@card)
      puts "Ahhh...you got me. Here you go!"
      @my_hand.shift(@cpu_hand[@card]) # ****Here's the error(line:21)
      print_hand
      else
      puts "Go fish!"
      @my_hand.shift(@card_deck[1])
      print_hand
      end
      end


      puts "There are #{card_deck.length} cards in this deck."
      puts "Welcome to Go-Fish."
      print "Your name please: "
      player_name = $stdin.gets.chomp.capitalize

      puts "Ok #{player_name}, lets get this deck shuffled..."
      #sleep(1)
      # shuffles card_deck using .shuffle method
      card_deck = card_deck.shuffle
      puts "Cards are perfectly shuffled!"
      #sleep(1)
      puts "Dealing cards..."
      #sleep(1)
      # assigns first 7 cards to user, removes from card_deck
      @my_hand = Array.new
      @my_hand = card_deck.shift(7)
      # assigns next 7 cards to CPU, removes from card_deck
      @cpu_hand = Array.new
      @cpu_hand = card_deck.shift(7)

      print_hand

      until card_deck.length < 1 || @cpu_hand.length < 1 || @my_hand.length < 1
      player_turn
      end

      puts "GAME OVER!"









      share|improve this question
















      I'm trying to create a replica of Go Fish to help me learn more about arrays and hashes, and just how to go about structuring data. I'm on day two and have what looks to be much closer to the end goal. Keep in mind, I'm new to this. Anyway, here's the problem I'm running into:




      => gofish.rb:21:in `player_turn': no implicit conversion of String into Integer (TypeError)




      I understand why I'm getting the error, but I can't figure out how to use the .shift method without giving an index number. I would like to select which object to shift based on the value instead. So, if I correctly guess do you have an 'ace of spades', the card is removed from the cpu_hand array and is added to the my_hand array. With that said, I would just like to know the best way to go about this.



      Here's my script:



      card_values = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
      'eight', 'nine', 'ten', 'jack', 'queen', 'king']
      suits = ['spades', 'diamonds', 'hearts', 'clubs']

      # creates array objects with every value and suit possible (full deck)
      card_deck = card_values.product(suits).collect{|card, suit| "#{card} of #{suit}"}

      def print_hand
      puts "Your hand: #{@my_hand.join(', ')}."
      end

      def player_turn
      puts "You go first!"
      puts "Do you have a..."
      puts @cpu_hand.join(', ')
      print "> "
      @card = $stdin.gets.chomp.downcase
      # if cpu has the card requested give it to the player and add to their array
      if @cpu_hand.include?(@card)
      puts "Ahhh...you got me. Here you go!"
      @my_hand.shift(@cpu_hand[@card]) # ****Here's the error(line:21)
      print_hand
      else
      puts "Go fish!"
      @my_hand.shift(@card_deck[1])
      print_hand
      end
      end


      puts "There are #{card_deck.length} cards in this deck."
      puts "Welcome to Go-Fish."
      print "Your name please: "
      player_name = $stdin.gets.chomp.capitalize

      puts "Ok #{player_name}, lets get this deck shuffled..."
      #sleep(1)
      # shuffles card_deck using .shuffle method
      card_deck = card_deck.shuffle
      puts "Cards are perfectly shuffled!"
      #sleep(1)
      puts "Dealing cards..."
      #sleep(1)
      # assigns first 7 cards to user, removes from card_deck
      @my_hand = Array.new
      @my_hand = card_deck.shift(7)
      # assigns next 7 cards to CPU, removes from card_deck
      @cpu_hand = Array.new
      @cpu_hand = card_deck.shift(7)

      print_hand

      until card_deck.length < 1 || @cpu_hand.length < 1 || @my_hand.length < 1
      player_turn
      end

      puts "GAME OVER!"






      arrays ruby project






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 20:19









      halfer

      14.6k758112




      14.6k758112










      asked Nov 24 '18 at 1:58









      KyleKyle

      103




      103
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In your error line,



          @my_hand.shift(@cpu_hand[@card])


          your crash is caused by attempting to index into the @cpu_hand array with a string (@card). The second issue is trying to call shift using a string as a parameter. Both of these need to be integers. shift removes the first element (or first n elements if an integer parameter is specified); this seems appropriate for taking an item from card_deck, but you'd want push, unshift, or << to add an element to @my_hand. You'll also need a method to remove the specified card from @cpu_hand, delete:



          if @cpu_hand.include?(@card)
          puts "Ahhh...you got me. Here you go!"
          @my_hand << @cpu_hand.delete(@card) # transfer a card from CPU to my hand
          print_hand
          else
          puts "Go fish!"
          @my_hand << card_deck.shift # take the first card from the deck (could also be pop?)
          print_hand
          end


          However, card_deck is not a global like your other variables, so you'll need to pass it into the function and change the function header to def player_turn card_deck, or make it global. But making everything global is generally considered poor design because data can be mutated from anywhere, leading to difficult-to-find bugs. Consider writing classes such as Hand and GoFish (for example) that encapsulate all of the logic necessary to represent reusable pieces of your game.



          I recommend taking a read through the array docs. You'll surely find some cool methods and learn a few tidbits.






          share|improve this answer
























          • Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

            – Kyle
            Nov 24 '18 at 20:15











          • You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

            – ggorlen
            Nov 24 '18 at 20:36











          • Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

            – ggorlen
            Nov 24 '18 at 20:41













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454562%2fruby-program-returns-error-implicit-conversion-of-string-into-integer-typeerro%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          In your error line,



          @my_hand.shift(@cpu_hand[@card])


          your crash is caused by attempting to index into the @cpu_hand array with a string (@card). The second issue is trying to call shift using a string as a parameter. Both of these need to be integers. shift removes the first element (or first n elements if an integer parameter is specified); this seems appropriate for taking an item from card_deck, but you'd want push, unshift, or << to add an element to @my_hand. You'll also need a method to remove the specified card from @cpu_hand, delete:



          if @cpu_hand.include?(@card)
          puts "Ahhh...you got me. Here you go!"
          @my_hand << @cpu_hand.delete(@card) # transfer a card from CPU to my hand
          print_hand
          else
          puts "Go fish!"
          @my_hand << card_deck.shift # take the first card from the deck (could also be pop?)
          print_hand
          end


          However, card_deck is not a global like your other variables, so you'll need to pass it into the function and change the function header to def player_turn card_deck, or make it global. But making everything global is generally considered poor design because data can be mutated from anywhere, leading to difficult-to-find bugs. Consider writing classes such as Hand and GoFish (for example) that encapsulate all of the logic necessary to represent reusable pieces of your game.



          I recommend taking a read through the array docs. You'll surely find some cool methods and learn a few tidbits.






          share|improve this answer
























          • Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

            – Kyle
            Nov 24 '18 at 20:15











          • You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

            – ggorlen
            Nov 24 '18 at 20:36











          • Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

            – ggorlen
            Nov 24 '18 at 20:41


















          0














          In your error line,



          @my_hand.shift(@cpu_hand[@card])


          your crash is caused by attempting to index into the @cpu_hand array with a string (@card). The second issue is trying to call shift using a string as a parameter. Both of these need to be integers. shift removes the first element (or first n elements if an integer parameter is specified); this seems appropriate for taking an item from card_deck, but you'd want push, unshift, or << to add an element to @my_hand. You'll also need a method to remove the specified card from @cpu_hand, delete:



          if @cpu_hand.include?(@card)
          puts "Ahhh...you got me. Here you go!"
          @my_hand << @cpu_hand.delete(@card) # transfer a card from CPU to my hand
          print_hand
          else
          puts "Go fish!"
          @my_hand << card_deck.shift # take the first card from the deck (could also be pop?)
          print_hand
          end


          However, card_deck is not a global like your other variables, so you'll need to pass it into the function and change the function header to def player_turn card_deck, or make it global. But making everything global is generally considered poor design because data can be mutated from anywhere, leading to difficult-to-find bugs. Consider writing classes such as Hand and GoFish (for example) that encapsulate all of the logic necessary to represent reusable pieces of your game.



          I recommend taking a read through the array docs. You'll surely find some cool methods and learn a few tidbits.






          share|improve this answer
























          • Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

            – Kyle
            Nov 24 '18 at 20:15











          • You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

            – ggorlen
            Nov 24 '18 at 20:36











          • Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

            – ggorlen
            Nov 24 '18 at 20:41
















          0












          0








          0







          In your error line,



          @my_hand.shift(@cpu_hand[@card])


          your crash is caused by attempting to index into the @cpu_hand array with a string (@card). The second issue is trying to call shift using a string as a parameter. Both of these need to be integers. shift removes the first element (or first n elements if an integer parameter is specified); this seems appropriate for taking an item from card_deck, but you'd want push, unshift, or << to add an element to @my_hand. You'll also need a method to remove the specified card from @cpu_hand, delete:



          if @cpu_hand.include?(@card)
          puts "Ahhh...you got me. Here you go!"
          @my_hand << @cpu_hand.delete(@card) # transfer a card from CPU to my hand
          print_hand
          else
          puts "Go fish!"
          @my_hand << card_deck.shift # take the first card from the deck (could also be pop?)
          print_hand
          end


          However, card_deck is not a global like your other variables, so you'll need to pass it into the function and change the function header to def player_turn card_deck, or make it global. But making everything global is generally considered poor design because data can be mutated from anywhere, leading to difficult-to-find bugs. Consider writing classes such as Hand and GoFish (for example) that encapsulate all of the logic necessary to represent reusable pieces of your game.



          I recommend taking a read through the array docs. You'll surely find some cool methods and learn a few tidbits.






          share|improve this answer













          In your error line,



          @my_hand.shift(@cpu_hand[@card])


          your crash is caused by attempting to index into the @cpu_hand array with a string (@card). The second issue is trying to call shift using a string as a parameter. Both of these need to be integers. shift removes the first element (or first n elements if an integer parameter is specified); this seems appropriate for taking an item from card_deck, but you'd want push, unshift, or << to add an element to @my_hand. You'll also need a method to remove the specified card from @cpu_hand, delete:



          if @cpu_hand.include?(@card)
          puts "Ahhh...you got me. Here you go!"
          @my_hand << @cpu_hand.delete(@card) # transfer a card from CPU to my hand
          print_hand
          else
          puts "Go fish!"
          @my_hand << card_deck.shift # take the first card from the deck (could also be pop?)
          print_hand
          end


          However, card_deck is not a global like your other variables, so you'll need to pass it into the function and change the function header to def player_turn card_deck, or make it global. But making everything global is generally considered poor design because data can be mutated from anywhere, leading to difficult-to-find bugs. Consider writing classes such as Hand and GoFish (for example) that encapsulate all of the logic necessary to represent reusable pieces of your game.



          I recommend taking a read through the array docs. You'll surely find some cool methods and learn a few tidbits.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 2:36









          ggorlenggorlen

          7,1883825




          7,1883825













          • Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

            – Kyle
            Nov 24 '18 at 20:15











          • You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

            – ggorlen
            Nov 24 '18 at 20:36











          • Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

            – ggorlen
            Nov 24 '18 at 20:41





















          • Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

            – Kyle
            Nov 24 '18 at 20:15











          • You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

            – ggorlen
            Nov 24 '18 at 20:36











          • Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

            – ggorlen
            Nov 24 '18 at 20:41



















          Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

          – Kyle
          Nov 24 '18 at 20:15





          Thank you so much for your response, very helpful! Right now I'm really struggling with how to organize my code into classes. Do you know of any helpful resources for understanding how to "encapsulate all the logic necessary to represent reusable pieces" as you put it? I feel like at this point I'm just throwing together pieces of code and making things work little by little. Also, would creating classes eliminate the need to create instance variables, is that what you meant? I thought glabal variables started with a $.

          – Kyle
          Nov 24 '18 at 20:15













          You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

          – ggorlen
          Nov 24 '18 at 20:36





          You're right: globals in Ruby are technically $-prefixed. But without a class, @-prefixed "instance" variables are visible inside any child function scope which is in a classical sense global. If you remove the @, these variables are local to the main function and you'd have to pass them as parameters. This would be better practice. But the next step after that is to group the functions logically: a Deck class might have shuffle and draw functions, for example, while a GoFish class might have a cpu_turn function.

          – ggorlen
          Nov 24 '18 at 20:36













          Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

          – ggorlen
          Nov 24 '18 at 20:41







          Part of the confusion might be that @-prefixed variables are usually seen inside classes--you'll still need these variables, except their scope will be limited to the class and they'll be unique to each instance of the class you create. My suggestion is to start by removing all @ variables from your code and pass all data as parameters. After that, you can refactor to classes if you wish. All this is entirely optional, but be warned that by not thinking in terms of design, you can wind up with a tangled mess that's hard to unwind (but that'd still be a worthwhile learning experience!).

          – ggorlen
          Nov 24 '18 at 20:41






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454562%2fruby-program-returns-error-implicit-conversion-of-string-into-integer-typeerro%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen