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 199407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:01:15+00:00 2026-05-11T17:01:15+00:00

I expected the following code to print 8, 111 and 999. I supposed that

  • 0

I expected the following code to print “8”, “111” and “999”. I supposed that each a, b, c and d points to the same memory location. If I change the location through one of them, why would the other not to change? Clearly, my logic is poor, or I overlooked something. It prints “7”, “7” and “8”, instead.

Why?

a=b=c=d=7
     b  =  8
     puts d

     c  = 111
     puts a

     d  =  999
     puts b

[Clarification]

The reason for my confusion is the example in the book (page 20). They change there similarly the values, but they get the results I suggested above. Are we speaking of the same issue?

  • 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-05-11T17:01:15+00:00Added an answer on May 11, 2026 at 5:01 pm
    a=b=c=d=7
    # a, b, c and d points to the same integer object "7"
         b  =  8
    # b now points to a new object "8"
    # "=" does not change the value of the pointer integer, 
    # it assings a new reference like in the line above
         puts d
    # obviously, d still points to "7"
    
         c  = 111
    # c now points to another integer object "111"
         puts a
    # a still points to "7"
    
         d  =  999
    # d now points to a new integer object "999"
         puts b
    # b still points to "8"
    

    in Ruby, the Integer object is immutable so you cannot assign an Integer to multiple reference and change its value after.

    As @pts suggested, you should use an array to wrap your Integer reference because Arrays are mutable to you are able to change the value after.

    a=b=c=d=[7]
    b[0] = 8
    puts d[0]
    c[0] = 111
    puts a[0]
    d[0] = 999
    puts b[0]
    

    CLARIFICATION:

    If you come from a C++ background, it may be strange because C++ does 2 things with the same syntax, assigning the reference and changing the value referenced.

    int a = 10; // creates an int on the stack with value 10
    int& b = a; // creates a reference to an int and references the a variable
    b = 5; // change the value referenced by b (so a) to 5
    // a and b now hold the value 5
    

    In Ruby, reference are mutable and integers are not (exactly the contrary to C++). So assigning a reference will actually change the reference and not the referenced value.

    Another solution would be to create a class that is a mutable integer:

    class MutableInteger
      attr_writer :value
      def initialize(value)
        @value = value
      end
      def inspect
        value
      end
      def to_i
        value
      end
      def to_s
        value
      end
    end
    
    a = b = MutableInteger.new(10)
    a.value = 5
    puts b
    # prints 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: url = 'abcdc.com' print(url.strip('.com')) I expected: abcdc I got:
Doing an ajax get request works as expected using the following code: $.ajax({ type:
The following code works as expected on CentOS and Ubuntu O/s but not on
The following code fails in 'Evaluate' with: This expression was expected to have type
The following code is throwing two alerts as expected in IE but not in
The following code converts a PDF page into a JPEG. It runs as expected
In the following code, the first log statement shows a decimal as expected, but
I have the following code: $('.breadcrumb:contains(,)').hide(); It does as expected and hides the entire
The following F# code works as I expected, printing `Matched as 'A': let (|Char|_|)
I have the following recursive code and it doesn't behave as expected (see details

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.