Note that there are some explanatory texts on larger screens.

plurals
  1. POBasic issue with variables
    primarykey
    data
    text
    <p>I feel like I'm missing something very, very fundamental, so, to avoid tearing out more hair, I come here to ask why p1, p1t and p1t2 are getting modified on with "hand = newgame.view p1". I don't even know why p1 is getting modified, so yeah I'm totally baffled here. Any help would be tremendously appreciated. </p> <pre><code># encoding: utf-8 class Cards def view hand x = 0 hand.each do |card| if card[0] == 's' card[0] = '♠' elsif card[0] == 'd' card[0] = '♦' elsif card[0] == 'h' card[0] = '♥' elsif card[0] == 'c' card[0] = '♣' else #nil end hand[x] = card x = x + 1 end prettyhand = '' hand.each do |card| prettyhand = prettyhand + card[0] + card[1] + ' ' end return prettyhand end#view end#Cards ########### deck = 0 up = 0 p1 = 0 p2 = 0 newgame = Cards.new p1 = [["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]] p1t = [] p1t2 = [] hand = '' p1.each do |card| p1t.push card end p1t.each do |card| p1t2.push card end p '----------------------------------' p 'fresh p1:' p p1 p p1t p p1t2 hand = newgame.view p1 p 'unfresh p1:'#why is this changing?? p p1 p p1t p p1t2 p '----------------------------------' </code></pre> <p>output:</p> <pre><code>"----------------------------------" "fresh p1:" [["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]] [["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]] [["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]] "unfresh p1:" [["♣", "4"], ["♠", "4"], ["♦", "q"], ["♣", "8"], ["♠", "5"], ["♠", "q"], ["♥", "3"], ["♣", "10"]] [["♣", "4"], ["♠", "4"], ["♦", "q"], ["♣", "8"], ["♠", "5"], ["♠", "q"], ["♥", "3"], ["♣", "10"]] [["♣", "4"], ["♠", "4"], ["♦", "q"], ["♣", "8"], ["♠", "5"], ["♠", "q"], ["♥", "3"], ["♣", "10"]] "----------------------------------" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload