Hi, all:
I am a new user of Ruby. Could anyone please tell me why in the
follwoing code, the string object is copied while the array is not. To
my understanding, dup and clone both should do shallow copy.
Thanks a lot.
class A
attr_accessor :str, :ary
def initialize
@str = "hello"
@ary =[1,2,3,4]
end
def to_s
puts "Str: #{@str}, Array: #{@ary} \n"
end
end
a = A.new
b = a.dup
a.to_s ->Str: hello, Array: 1234
b.to_s ->Str: hello, Array: 1234
b.str = "Hello"
b.ary[0]="cat"
a.to_s ->Str: hello, Array:cat234
b.to_s ->Str: Hello, Array:cat234