ruby variable as same object (pointers?)

前端 未结 7 1029
-上瘾入骨i
-上瘾入骨i 2021-01-01 03:22
>> a = 5
=> 5
>> b = a
=> 5
>> b = 4
=> 4
>> a
=> 5

how can I set \'b\' to actually be \'a\' so that in the exa

7条回答
  •  情歌与酒
    2021-01-01 03:46

    Just for the sake of reference.

    >> a = 5
    => 5
    >> a.object_id
    => 11
    >> b = a
    => 5
    >> b.object_id
    => 11
    >> b = 4
    => 4
    >> b.object_id
    => 9
    >> a.object_id
    => 11
    # We did change the Fixnum b Object.
    >> Fixnum.superclass
    => Integer
    >> Integer.superclass
    => Numeric
    >> Numeric.superclass
    => Object
    >> Object.superclass
    => BasicObject
    >> BasicObject.superclass
    => nil
    

    I hope this gives us all a little better understanding about objects in Ruby.

提交回复
热议问题