Ruby 中的对象赋值
来自 C++ 背景的我对 Ruby 中的对象分配感到好奇。对于以下对象分配应考虑哪些因素(如果有):
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您熟悉 C++,那么您可能需要将 Ruby 中的每个变量、实例或其他变量视为对另一个对象的引用。由于 Ruby 中的所有内容都是对象,甚至是 NilClass 类型的
nil
,因此这在任何情况下都适用。要确定您引用的对象,可以使用
object_id
方法来区分。这类似于在 C++ 中使用&
转换为指针。考虑一下:
由于
a
是对该字符串的引用,而b
是a
的副本,因此它们实际上是对同一字符串的不同引用目的。这很重要,因为修改对象的操作会同等影响对该对象的所有引用:
但是,创建新对象的操作不会修改所有副本:
Ruby 中的许多方法都由
!< 标识/code> 来区分就地版本和新副本版本,但情况并非总是如此,因此您必须熟悉每种方法才能确定。
通常,分配会将旧引用替换为新引用,因此根据经验,
=
将替换旧引用。这适用于+=
、-=
、||=
、&&=
等。编辑:根据 Phrogz 关于使用
ObjectSpace._id2ref(object_id)
将对象标识符转换为对象的评论进行了更新。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:
Since
a
is a reference to that string, andb
is a copy ofa
, 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:
However, operations that create a new object will not modify all copies:
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.由于 ruby 中一切都是对象,因此赋值始终是通过引用进行的。
因此,以您的类作为输入,以下将是多个操作的输出:
Since everything is an object in ruby, assignment is always by reference.
So, taking your class as an input, the following will be the output for several operations:
我将其重写为:
这样,您实际上是在类中使用由
attr_accessor
定义的 getter/setter 方法当您分配
q = n
时,q 只是引用相同的方法为 n 设置的内存位置。该对象未被复制。I would rewrite this as:
This way you're actually using the getter/setter methods defined by
attr_accessor
within your classWhen you assign
q = n
, q just references the same memory location that was set for n. The object isn't copied.