Re: [Rails] Need help to understand the difference between #dup and #clone while working singleton methods

2013-09-22 Thread Narrenblut .
I'd think, the difference in following example shows easier, where is the difference in you case, too foo = [1,2] bar= foo.dup foo<< 3 puts bar.inspect foo= [ [1,2] ] bar = foo.dup foo[0] << 3 puts bar.inspect Try it with clone - and you recognize the difference between deep copy clone and sh

[Rails] Need help to understand the difference between #dup and #clone while working singleton methods

2013-09-21 Thread Love U Ruby
Look the below code with Object#clone: foo = Object.new def foo.bar "hi" end baz = foo.clone foo.bar # => "hi" baz.bar # => "hi" Now with Object#dup foo = Object.new def foo.bar "hi" end baz = foo.dup foo.bar # => "hi" baz.bar # `': undefined method `bar' for # (NoMethodError) Why the below