undefined method 'push' for string












1















I'm working on a project to replicate Go Fish so I can learn more about structuring data and how to properly use arrays. I'm having a bit of trouble trying to figure out why I keep getting this error. I tried changing the variables into instance variable, this didn't seem to help much. I was hoping someone could take a peek at my code and point me in the right direction.
**Please feel free to give any suggestions, even if it's unrelated to the question. I don't know if I'm approaching this correctly.



card_deck = ["ace of spades", "ace of diamonds", "ace of clubs", "ace of hearts",
"two of spades", "two of diamonds", "two of hearts", "two of clubs",
"three of spades", "three of diamonds", "three of hearts", "three of clubs",
"four of spades", "four of diamonds", "four of hearts", "four of clubs",
"five of spades", "five of diamonds", "four of hearts", "five of clubs",
"six of spades", "six of diamonds", "six of hearts", "six of clubs",
"seven of spades", "seven of diamonds", "seven of hearts", "seven of clubs",
"eight of spades", "eight of diamonds", "eight of hearts", "eight of clubs",
"nine of spades", "nine of diamonds", "nine of hearts", "nine of clubs",
"ten of spades", "ten of diamonds", "ten of hearts", "ten of clubs",
"jack of spades", "jack of diamonds", "jack of hearts", "jack of clubs",
"queen of spades", "queen of diamonds", "queen of hearts", "queen of clubs",
"king of spades", "king of diamonds", "king of hearts", "king of clubs"]

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
@my_hand = Array.new
@my_hand = card_deck[0..6].join(', ')
# assigns next 7 cards to CPU
@cpu_hand = Array.new
@cpu_hand = card_deck[7..13]

# removes first 14 cards from the deck (0-13)
@card_deck = card_deck.drop(13)

puts "Here's your hand: #{@my_hand}."
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)
@my_hand.push(@card)
else
puts "Go fish!"
end









share|improve this question























  • @my_hand = card_deck[0..6].join(', ') turns @my_hand to a string

    – AbM
    Nov 23 '18 at 3:07











  • the .join method?

    – Kyle
    Nov 23 '18 at 3:10











  • Ok, I got it to work but I have a follow up question if you don't mind. If I never declare "@my_hand = @myhand.join(', ')" , then I'll have to repeat the code every time I want to print the value. Is there a way around this? Should I just create a second variable such as "@my_hand_printed" so that I can avoid this problem down the road?

    – Kyle
    Nov 23 '18 at 3:15













  • Define a method that does it.

    – iGian
    Nov 23 '18 at 4:23











  • Thanks to both of you. I appreciate the help!

    – Kyle
    Nov 23 '18 at 4:46
















1















I'm working on a project to replicate Go Fish so I can learn more about structuring data and how to properly use arrays. I'm having a bit of trouble trying to figure out why I keep getting this error. I tried changing the variables into instance variable, this didn't seem to help much. I was hoping someone could take a peek at my code and point me in the right direction.
**Please feel free to give any suggestions, even if it's unrelated to the question. I don't know if I'm approaching this correctly.



card_deck = ["ace of spades", "ace of diamonds", "ace of clubs", "ace of hearts",
"two of spades", "two of diamonds", "two of hearts", "two of clubs",
"three of spades", "three of diamonds", "three of hearts", "three of clubs",
"four of spades", "four of diamonds", "four of hearts", "four of clubs",
"five of spades", "five of diamonds", "four of hearts", "five of clubs",
"six of spades", "six of diamonds", "six of hearts", "six of clubs",
"seven of spades", "seven of diamonds", "seven of hearts", "seven of clubs",
"eight of spades", "eight of diamonds", "eight of hearts", "eight of clubs",
"nine of spades", "nine of diamonds", "nine of hearts", "nine of clubs",
"ten of spades", "ten of diamonds", "ten of hearts", "ten of clubs",
"jack of spades", "jack of diamonds", "jack of hearts", "jack of clubs",
"queen of spades", "queen of diamonds", "queen of hearts", "queen of clubs",
"king of spades", "king of diamonds", "king of hearts", "king of clubs"]

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
@my_hand = Array.new
@my_hand = card_deck[0..6].join(', ')
# assigns next 7 cards to CPU
@cpu_hand = Array.new
@cpu_hand = card_deck[7..13]

# removes first 14 cards from the deck (0-13)
@card_deck = card_deck.drop(13)

puts "Here's your hand: #{@my_hand}."
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)
@my_hand.push(@card)
else
puts "Go fish!"
end









share|improve this question























  • @my_hand = card_deck[0..6].join(', ') turns @my_hand to a string

    – AbM
    Nov 23 '18 at 3:07











  • the .join method?

    – Kyle
    Nov 23 '18 at 3:10











  • Ok, I got it to work but I have a follow up question if you don't mind. If I never declare "@my_hand = @myhand.join(', ')" , then I'll have to repeat the code every time I want to print the value. Is there a way around this? Should I just create a second variable such as "@my_hand_printed" so that I can avoid this problem down the road?

    – Kyle
    Nov 23 '18 at 3:15













  • Define a method that does it.

    – iGian
    Nov 23 '18 at 4:23











  • Thanks to both of you. I appreciate the help!

    – Kyle
    Nov 23 '18 at 4:46














1












1








1








I'm working on a project to replicate Go Fish so I can learn more about structuring data and how to properly use arrays. I'm having a bit of trouble trying to figure out why I keep getting this error. I tried changing the variables into instance variable, this didn't seem to help much. I was hoping someone could take a peek at my code and point me in the right direction.
**Please feel free to give any suggestions, even if it's unrelated to the question. I don't know if I'm approaching this correctly.



card_deck = ["ace of spades", "ace of diamonds", "ace of clubs", "ace of hearts",
"two of spades", "two of diamonds", "two of hearts", "two of clubs",
"three of spades", "three of diamonds", "three of hearts", "three of clubs",
"four of spades", "four of diamonds", "four of hearts", "four of clubs",
"five of spades", "five of diamonds", "four of hearts", "five of clubs",
"six of spades", "six of diamonds", "six of hearts", "six of clubs",
"seven of spades", "seven of diamonds", "seven of hearts", "seven of clubs",
"eight of spades", "eight of diamonds", "eight of hearts", "eight of clubs",
"nine of spades", "nine of diamonds", "nine of hearts", "nine of clubs",
"ten of spades", "ten of diamonds", "ten of hearts", "ten of clubs",
"jack of spades", "jack of diamonds", "jack of hearts", "jack of clubs",
"queen of spades", "queen of diamonds", "queen of hearts", "queen of clubs",
"king of spades", "king of diamonds", "king of hearts", "king of clubs"]

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
@my_hand = Array.new
@my_hand = card_deck[0..6].join(', ')
# assigns next 7 cards to CPU
@cpu_hand = Array.new
@cpu_hand = card_deck[7..13]

# removes first 14 cards from the deck (0-13)
@card_deck = card_deck.drop(13)

puts "Here's your hand: #{@my_hand}."
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)
@my_hand.push(@card)
else
puts "Go fish!"
end









share|improve this question














I'm working on a project to replicate Go Fish so I can learn more about structuring data and how to properly use arrays. I'm having a bit of trouble trying to figure out why I keep getting this error. I tried changing the variables into instance variable, this didn't seem to help much. I was hoping someone could take a peek at my code and point me in the right direction.
**Please feel free to give any suggestions, even if it's unrelated to the question. I don't know if I'm approaching this correctly.



card_deck = ["ace of spades", "ace of diamonds", "ace of clubs", "ace of hearts",
"two of spades", "two of diamonds", "two of hearts", "two of clubs",
"three of spades", "three of diamonds", "three of hearts", "three of clubs",
"four of spades", "four of diamonds", "four of hearts", "four of clubs",
"five of spades", "five of diamonds", "four of hearts", "five of clubs",
"six of spades", "six of diamonds", "six of hearts", "six of clubs",
"seven of spades", "seven of diamonds", "seven of hearts", "seven of clubs",
"eight of spades", "eight of diamonds", "eight of hearts", "eight of clubs",
"nine of spades", "nine of diamonds", "nine of hearts", "nine of clubs",
"ten of spades", "ten of diamonds", "ten of hearts", "ten of clubs",
"jack of spades", "jack of diamonds", "jack of hearts", "jack of clubs",
"queen of spades", "queen of diamonds", "queen of hearts", "queen of clubs",
"king of spades", "king of diamonds", "king of hearts", "king of clubs"]

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
@my_hand = Array.new
@my_hand = card_deck[0..6].join(', ')
# assigns next 7 cards to CPU
@cpu_hand = Array.new
@cpu_hand = card_deck[7..13]

# removes first 14 cards from the deck (0-13)
@card_deck = card_deck.drop(13)

puts "Here's your hand: #{@my_hand}."
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)
@my_hand.push(@card)
else
puts "Go fish!"
end






arrays ruby methods






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 3:05









KyleKyle

103




103













  • @my_hand = card_deck[0..6].join(', ') turns @my_hand to a string

    – AbM
    Nov 23 '18 at 3:07











  • the .join method?

    – Kyle
    Nov 23 '18 at 3:10











  • Ok, I got it to work but I have a follow up question if you don't mind. If I never declare "@my_hand = @myhand.join(', ')" , then I'll have to repeat the code every time I want to print the value. Is there a way around this? Should I just create a second variable such as "@my_hand_printed" so that I can avoid this problem down the road?

    – Kyle
    Nov 23 '18 at 3:15













  • Define a method that does it.

    – iGian
    Nov 23 '18 at 4:23











  • Thanks to both of you. I appreciate the help!

    – Kyle
    Nov 23 '18 at 4:46



















  • @my_hand = card_deck[0..6].join(', ') turns @my_hand to a string

    – AbM
    Nov 23 '18 at 3:07











  • the .join method?

    – Kyle
    Nov 23 '18 at 3:10











  • Ok, I got it to work but I have a follow up question if you don't mind. If I never declare "@my_hand = @myhand.join(', ')" , then I'll have to repeat the code every time I want to print the value. Is there a way around this? Should I just create a second variable such as "@my_hand_printed" so that I can avoid this problem down the road?

    – Kyle
    Nov 23 '18 at 3:15













  • Define a method that does it.

    – iGian
    Nov 23 '18 at 4:23











  • Thanks to both of you. I appreciate the help!

    – Kyle
    Nov 23 '18 at 4:46

















@my_hand = card_deck[0..6].join(', ') turns @my_hand to a string

– AbM
Nov 23 '18 at 3:07





@my_hand = card_deck[0..6].join(', ') turns @my_hand to a string

– AbM
Nov 23 '18 at 3:07













the .join method?

– Kyle
Nov 23 '18 at 3:10





the .join method?

– Kyle
Nov 23 '18 at 3:10













Ok, I got it to work but I have a follow up question if you don't mind. If I never declare "@my_hand = @myhand.join(', ')" , then I'll have to repeat the code every time I want to print the value. Is there a way around this? Should I just create a second variable such as "@my_hand_printed" so that I can avoid this problem down the road?

– Kyle
Nov 23 '18 at 3:15







Ok, I got it to work but I have a follow up question if you don't mind. If I never declare "@my_hand = @myhand.join(', ')" , then I'll have to repeat the code every time I want to print the value. Is there a way around this? Should I just create a second variable such as "@my_hand_printed" so that I can avoid this problem down the road?

– Kyle
Nov 23 '18 at 3:15















Define a method that does it.

– iGian
Nov 23 '18 at 4:23





Define a method that does it.

– iGian
Nov 23 '18 at 4:23













Thanks to both of you. I appreciate the help!

– Kyle
Nov 23 '18 at 4:46





Thanks to both of you. I appreciate the help!

– Kyle
Nov 23 '18 at 4:46












2 Answers
2






active

oldest

votes


















1














push method



a = [ "a", "b", "c" ]
a.push("d", "e", "f")
#=> ["a", "b", "c", "d", "e", "f"]
[1, 2, 3,].push(4).push(5)
#=> [1, 2, 3, 4, 5]


join method



[ "a", "b", "c" ].join        #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"





share|improve this answer
























  • Thanks, your description was on point!

    – Kyle
    Nov 23 '18 at 21:11



















0














If you want to remove first 7 cards from deck and place them in the players hand, you need to do this operation:



@my_hand = card_deck.shift(7)


After that you can give next 7 cards to CPU:



@cpu_hand = card_deck.shift(7)


There is no need to make drop after that.
Also, drop(13) will drop exactly 13 elements, not 14 (0-13).



Additionally, you may like shorter version of deck initialization:



cards = ['ace','one','two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'clubs', 'diamonds','hearts']
card_deck = cards.product(suits).collect{|x,y| "#{x} of #{y}"}





share|improve this answer


























  • Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

    – Kyle
    Nov 23 '18 at 21:10











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%2f53440182%2fundefined-method-push-for-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














push method



a = [ "a", "b", "c" ]
a.push("d", "e", "f")
#=> ["a", "b", "c", "d", "e", "f"]
[1, 2, 3,].push(4).push(5)
#=> [1, 2, 3, 4, 5]


join method



[ "a", "b", "c" ].join        #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"





share|improve this answer
























  • Thanks, your description was on point!

    – Kyle
    Nov 23 '18 at 21:11
















1














push method



a = [ "a", "b", "c" ]
a.push("d", "e", "f")
#=> ["a", "b", "c", "d", "e", "f"]
[1, 2, 3,].push(4).push(5)
#=> [1, 2, 3, 4, 5]


join method



[ "a", "b", "c" ].join        #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"





share|improve this answer
























  • Thanks, your description was on point!

    – Kyle
    Nov 23 '18 at 21:11














1












1








1







push method



a = [ "a", "b", "c" ]
a.push("d", "e", "f")
#=> ["a", "b", "c", "d", "e", "f"]
[1, 2, 3,].push(4).push(5)
#=> [1, 2, 3, 4, 5]


join method



[ "a", "b", "c" ].join        #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"





share|improve this answer













push method



a = [ "a", "b", "c" ]
a.push("d", "e", "f")
#=> ["a", "b", "c", "d", "e", "f"]
[1, 2, 3,].push(4).push(5)
#=> [1, 2, 3, 4, 5]


join method



[ "a", "b", "c" ].join        #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 9:09









adarshadarsh

12411




12411













  • Thanks, your description was on point!

    – Kyle
    Nov 23 '18 at 21:11



















  • Thanks, your description was on point!

    – Kyle
    Nov 23 '18 at 21:11

















Thanks, your description was on point!

– Kyle
Nov 23 '18 at 21:11





Thanks, your description was on point!

– Kyle
Nov 23 '18 at 21:11













0














If you want to remove first 7 cards from deck and place them in the players hand, you need to do this operation:



@my_hand = card_deck.shift(7)


After that you can give next 7 cards to CPU:



@cpu_hand = card_deck.shift(7)


There is no need to make drop after that.
Also, drop(13) will drop exactly 13 elements, not 14 (0-13).



Additionally, you may like shorter version of deck initialization:



cards = ['ace','one','two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'clubs', 'diamonds','hearts']
card_deck = cards.product(suits).collect{|x,y| "#{x} of #{y}"}





share|improve this answer


























  • Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

    – Kyle
    Nov 23 '18 at 21:10
















0














If you want to remove first 7 cards from deck and place them in the players hand, you need to do this operation:



@my_hand = card_deck.shift(7)


After that you can give next 7 cards to CPU:



@cpu_hand = card_deck.shift(7)


There is no need to make drop after that.
Also, drop(13) will drop exactly 13 elements, not 14 (0-13).



Additionally, you may like shorter version of deck initialization:



cards = ['ace','one','two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'clubs', 'diamonds','hearts']
card_deck = cards.product(suits).collect{|x,y| "#{x} of #{y}"}





share|improve this answer


























  • Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

    – Kyle
    Nov 23 '18 at 21:10














0












0








0







If you want to remove first 7 cards from deck and place them in the players hand, you need to do this operation:



@my_hand = card_deck.shift(7)


After that you can give next 7 cards to CPU:



@cpu_hand = card_deck.shift(7)


There is no need to make drop after that.
Also, drop(13) will drop exactly 13 elements, not 14 (0-13).



Additionally, you may like shorter version of deck initialization:



cards = ['ace','one','two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'clubs', 'diamonds','hearts']
card_deck = cards.product(suits).collect{|x,y| "#{x} of #{y}"}





share|improve this answer















If you want to remove first 7 cards from deck and place them in the players hand, you need to do this operation:



@my_hand = card_deck.shift(7)


After that you can give next 7 cards to CPU:



@cpu_hand = card_deck.shift(7)


There is no need to make drop after that.
Also, drop(13) will drop exactly 13 elements, not 14 (0-13).



Additionally, you may like shorter version of deck initialization:



cards = ['ace','one','two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
suits = ['spades', 'clubs', 'diamonds','hearts']
card_deck = cards.product(suits).collect{|x,y| "#{x} of #{y}"}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 10:42

























answered Nov 23 '18 at 9:55









Pavel OganesyanPavel Oganesyan

3,84143271




3,84143271













  • Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

    – Kyle
    Nov 23 '18 at 21:10



















  • Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

    – Kyle
    Nov 23 '18 at 21:10

















Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

– Kyle
Nov 23 '18 at 21:10





Thank you so much Pavel. I was also trying to wrap my head around how I should structure the deck and this helped tremendously!

– Kyle
Nov 23 '18 at 21:10


















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%2f53440182%2fundefined-method-push-for-string%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()