本文共 2741 字,大约阅读时间需要 9 分钟。
s1 = %Q/Hello, I'm Digoal.Zhou/s2 = s1 # 把一个String对象赋值给另一个对象, 其实是一次reference赋值s3 = s1.clone() # 使用clone()方法则不是reference操作, 而是重新创建了一个对象.puts(s1.class)puts(s2.class)puts("s1: #{s1}")puts("s2: #{s2}")puts(s1.object_id) # 看看这三个对象的object_id就知道了.puts(s2.object_id)puts(s3.object_id)s1.reverse!puts("s1: #{s1}") # 修改s1 后 , s2其实也修改了, 因为它们执行同一个值.puts("s2: #{s2}")puts("s3: #{s3}") # 显然s3是另一个对象, 修改s1,s2对它没反映.s2.capitalize!puts("s1: #{s1}")puts("s2: #{s2}")puts("s3: #{s3}")s1.reverse! << '. What\'s your name?'puts("s1: #{s1}")puts("s2: #{s2}")puts("s3: #{s3}")s2[0,6] = 'DIG' # 这是修改从0号index开始的6个字符.即"hello,"改为"DIG"puts("s1: #{s1}")puts("s2: #{s2}")puts("s3: #{s3}")puts(s1.object_id)puts(s2.object_id)puts(s3.object_id)执行结果 :
StringStrings1: Hello, I'm Digoal.Zhous2: Hello, I'm Digoal.Zhou165814681658146816581456s1: uohZ.laogiD m'I ,olleHs2: uohZ.laogiD m'I ,olleHs3: Hello, I'm Digoal.Zhous1: Uohz.laogid m'i ,ollehs2: Uohz.laogid m'i ,ollehs3: Hello, I'm Digoal.Zhous1: hello, i'm digoal.zhoU. What's your name?s2: hello, i'm digoal.zhoU. What's your name?s3: Hello, I'm Digoal.Zhous1: DIG i'm digoal.zhoU. What's your name?s2: DIG i'm digoal.zhoU. What's your name?s3: Hello, I'm Digoal.Zhou165814681658146816581456例如, Array类的对象也一样 :
a1 = Array.new([1,2,3,4,5])a2 = a1a3 = a1.clone()print("a1,#{a1}\n")print("a2,#{a2}\n")print("a3,#{a3}\n")puts(a1.object_id)puts(a2.object_id)puts(a3.object_id)a1[0] = "DIGOAL"print("a1,#{a1}\n")print("a2,#{a2}\n")print("a3,#{a3}\n")puts(a1.object_id)puts(a2.object_id)puts(a3.object_id)执行结果 :
a1,[1, 2, 3, 4, 5]a2,[1, 2, 3, 4, 5]a3,[1, 2, 3, 4, 5]155332801553328015533256a1,["DIGOAL", 2, 3, 4, 5]a2,["DIGOAL", 2, 3, 4, 5]a3,[1, 2, 3, 4, 5]155332801553328015533256
s1 = "Hello, I'm Digoal. What's your name?"s2 = s1puts(s1.object_id)puts(s2.object_id)s1 = "Hello, I'm Digoal. What's your name?" # 虽然这里赋予了相同的值, 但是实际上是新产生了一个对象ID.puts(s1.object_id)puts(s2.object_id)s1 << " Let's go." # 所以对s1改变时, s2不会受到影响. 因为它们指向的不是一个值了.puts(s1)puts(s2)执行结果 :
16121508161215081612141216121508Hello, I'm Digoal. What's your name? Let's go.Hello, I'm Digoal. What's your name?但是对于Fixnum类的对象, 和Array,String类不太一样, 只要值相同, object_id就相同.
f1 = 10f2 = f1puts(f1.class)puts(f2.class)puts(f1.object_id)puts(f2.object_id)f1 = 10puts(f1.object_id)puts(f2.object_id)执行结果 :
FixnumFixnum21212121注意仅限于Fixnum, 新赋予的Float值相同, object_id也不同 .
f1 = 10.0f2 = f1puts(f1.class)puts(f2.class)puts(f1.object_id)puts(f2.object_id)f1 = 10.0puts(f1.object_id)puts(f2.object_id)执行结果 :
FloatFloat16354536163545361635412816354536
转载地址:http://qouwa.baihongyu.com/