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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:45:02+00:00 2026-05-27T03:45:02+00:00

Coming from a c++ background I’m curious about object assignment in Ruby. What considerations

  • 0

Coming from a c++ background I’m curious about object assignment in Ruby. What considerations (if any) should be made for the following object assignments:

class MyClass

  attr_accessor :a, :b

  def initialize(a, b)
    @a = a
    @b = b
  end

  def some_method
    puts "#{self.a} #{self.b}"
  end
end

m = MyClass.new("first", "last")
n = MyClass.new("pizza", "hello")

q = n
q.some_method
  • 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-27T03:45:02+00:00Added an answer on May 27, 2026 at 3:45 am

    If you’re familiar with C++, then you might want to consider every variable in Ruby, instance or otherwise, as a reference to another object. Since everything in Ruby is an object, even nil, which is of type NilClass, this holds true under every circumstance.

    To determine which object you’re referencing, you can use the object_id method to differentiate. That’s similar to converting to a pointer using & in C++.

    Consider this:

    a = "foo"
    b = a
    
    a.object_id == b.object_id
    # => true
    

    Since a is a reference to that string, and b is a copy of a, then they are actually different references to the same object.

    This is important because operations that modify an object affect all references to that equally:

    a << "bar"
    # => "foobar"
    b
    # => "foobar"
    

    However, operations that create a new object will not modify all copies:

    a += "baz"
    # => "foobarbaz"
    b
    # => "foobar"
    

    Many methods in Ruby are identified by a ! to distinguish in-place versus new-copy versions, but this is not always the case, so you have to be familiar with each method in order to be sure.

    Generally an assignment will replace an old reference with a new one, so as a rule of thumb, = will replace old references. This applies to +=, -=, ||=, &&= and so on.

    Edit: Updated based on Phrogz’s comment about using ObjectSpace._id2ref(object_id) to convert an object identifier into an object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Coming from a Java background, I am a little perturbed by Ruby's completely blasé
I'm coming from a background whereby pointers should generally be compared with 'NULL' and
Coming from a background, I'm familiar with GUI editors that do a poor job
Coming from a background in Django, I often use template inheritance, where multiple templates
I'm coming from a background in ColdFusion, and finally moving onto something modern, so
When I was learning Java coming from a background of some 20 years of
Coming from a C# background the naming convention for variables and methods are usually
Coming from a desktop background I'm not sure exactly how to pass the exceptions
Coming from a java background, one of the things I am used to is
Coming from a Perl background, I have to say I prefer cpan Foo::Bar to

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.