ruby variable as same object (pointers?)

前端 未结 7 988
-上瘾入骨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:47

    You can use arrays:

    a = [5]
    b = a
    b[0] = 4
    puts a[0]  #=>  4
    

    This idea is based on this answer.

    0 讨论(0)
提交回复
热议问题