-
-
Save johro/2124f78284b3617f13d46e92e641dc73 to your computer and use it in GitHub Desktop.
Rubyの参照の値渡しのテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample | |
def array_change_all(v) | |
p 'value method start' | |
p v.object_id | |
v = [10, 2, 3] | |
p v.object_id | |
p 'value method end' | |
end | |
def array_change_one(v) | |
p 'value method start' | |
p v.object_id | |
v[0] = 10 | |
p v.object_id | |
p 'value method end' | |
end | |
def num(v) | |
p 'value method start' | |
p v.object_id | |
v = 10 | |
p v.object_id | |
p 'value method end' | |
end | |
end | |
p a = Sample.new | |
b = a | |
c = b | |
p d = Sample.new | |
p "a id : #{a.object_id}" | |
p "b id : #{b.object_id}" | |
p "c id : #{c.object_id}" | |
p "d id : #{d.object_id}" | |
p "a and b is identity? : #{a.eql?(b)}" | |
p "c and b is identity? : #{c.eql?(b)}" | |
p "a and d is same value? : #{a == d}" | |
p "a and d is identity? #{a.eql?(d)}" | |
p array = [1,2,3] | |
p "array id is : #{array.object_id}" | |
a.array_change_all(array) | |
p "return array value is : #{array}" | |
p "array id is : #{array.object_id}" | |
a.array_change_one(array) | |
p "return array value is : #{array}" | |
p "array id is : #{array.object_id}" | |
p int1 =1 | |
p "int1 id is : #{int1.object_id}" | |
a.num(int1) | |
p "int1 id is : #{int1.object_id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment