Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7807267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:48:05+00:00 2026-06-02T02:48:05+00:00

I feel like I’m missing something very, very fundamental, so, to avoid tearing out

  • 0

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.

# 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 '----------------------------------'

output:

"----------------------------------"
"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"]]
"----------------------------------"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:48:07+00:00Added an answer on June 2, 2026 at 2:48 am

    Arrays are passed by reference, not copied. Check out this snippet.

    a1 = [[1], [2], [3]]
    a2 = []
    
    a2 << a1.first # push a reference to the array
    a2 # => [[1]]
    
    
    a1[0][0] += 1 # => 2
    a1 # => [[2], [2], [3]] # a1 has changed
    a2 # => [[2]] # a2 changed too!
    
    # now push a copy of the array
    a2 = [a1.first.dup] # => [[2]]
    
    a1[0][0] += 1 # => 3
    a1 # => [[3], [2], [3]] # a1 has changed
    a2 # => [[2]] # a2 has not
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I am missing something very simple here, but this is the
I feel like I'm missing something very simple here. I have Eclipse set up
I feel like there's something rather basic I'm missing here, which I previously thought
I feel like I'm missing something here, but I have this datagrid which when
I feel like I might be overlooking something very extremely simple here, but I
I feel like I'm missing something simple here (as usual). I'm trying to read
I feel like I'm missing a fairly fundamental concept to WPF when it comes
I feel like this is a dumb question and I'm missing something, but I
I feel like i am completely missing something because I am not able to
I feel like this is easy but I am missing something... Using jQuery, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.