Object assignment and pointers

前端 未结 4 2011
予麋鹿
予麋鹿 2020-12-10 17:56

I am a little confused about object assignment and pointers in Ruby, and coded up this snippet to test my assumptions.

class Foo
    attr_accessor :one, :two         


        
4条回答
  •  星月不相逢
    2020-12-10 18:09

    You never deal with a copy. It's the same object in memory, but you just declare 2 references to it: in your first example: bar and beans point towards the same object in memory; and in your second example: foo and baz point to the same array in memory initially.

    Check out the 2 picture/drawings, in the Java tutorial page that explains the mechanism (it's the same as in Ruby) and those 2 pictures only, worth more than any explanation in words: http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

提交回复
热议问题