arr = ['arr',1,2,3]
a = [arr,'str_a']
b = [arr,'str_b']
c = [arr,'str_c']
d = [arr,'str_d']
# 对象引用
aa = a
# 浅拷贝
bb = b.clone
cc = c.dup
# 深拷贝
dd = Marshal.load(Marshal.dump(d))
a.push('push_a')
b.push('push_b')
c.push('push_c')
d.push('push_d')
arr.push('push_arr')
p a,aa
puts "\n"
p b,bb
puts "\n"
p c,cc
puts "\n"
p d,dd
=begin
[["arr", 1, 2, 3, "push_arr"], "str_a", "push_a"]
[["arr", 1, 2, 3, "push_arr"], "str_a", "push_a"]
[["arr", 1, 2, 3, "push_arr"], "str_b", "push_b"]
[["arr", 1, 2, 3, "push_arr"], "str_b"]
[["arr", 1, 2, 3, "push_arr"], "str_c", "push_c"]
[["arr", 1, 2, 3, "push_arr"], "str_c"]
[["arr", 1, 2, 3, "push_arr"], "str_d", "push_d"]
[["arr", 1, 2, 3], "str_d"]
=end
Ruby 赋值、浅拷贝和深拷贝
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转载请注明出处:http://www.jianshu.com/p/db57f7f70a5c本文出自Shawpoo的...
- ES6 变量声明与赋值:值传递、浅拷贝与深拷贝详解 ES6 为我们引入了 let 与 const 两种新的变量声明...
- x = {'course':['python','linux']} 1.直接赋值 y = x 传递原始对象的引用,...