Object assignment in Ruby

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

Answers


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.


Need Your Help

How can I get the top-most viewController when it implements multiple other classes?

ios objective-c

So far I've been able to get the top-most (the visible) viewController reliably using the following code:

Error: android.view.InflateException: Binary XML file line #26: Error inflating class com.android.camera.CropImageView

android android-imageview android-image

My application using Image croping and running under some of mobile, however it is giving following error specially while running with Samsung mobile version 4.0 or higher. Complete error log as un...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.