Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > clone and dup

Reply
Thread Tools

clone and dup

 
 
Keith P. Boruff
Guest
Posts: n/a
 
      01-26-2005
After reading about these two methods for the Object class in "Pragmatic
Programmer", I'm still having a hard time understanding the differences.
Can someone point me to some better info on these or explain it (I do
understand the differences between deep and shallow copying).

Also, in some of the std modules I've peeked at like Find for instance, this
is called for the args:

def find(*paths)
paths.collect! { |d| d.dup }
...
end

Why would one dup the args in place? I definitely don't understand this.

Thanks,
KPB
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-26-2005

"Keith P. Boruff" <(E-Mail Removed)> schrieb im Newsbeitrag
news:89KJd.989$(E-Mail Removed)...
> After reading about these two methods for the Object class in "Pragmatic
> Programmer", I'm still having a hard time understanding the differences.
> Can someone point me to some better info on these or explain it (I do
> understand the differences between deep and shallow copying).


They do both shallow copy.

Here's a difference:

>> s="foo".freeze

=> "foo"
>> s.frozen?

=> true
>> s.clone.frozen?

=> true
>> s.dup.frozen?

=> false

> Also, in some of the std modules I've peeked at like Find for instance,

this
> is called for the args:
>
> def find(*paths)
> paths.collect! { |d| d.dup }
> ...
> end
>
> Why would one dup the args in place? I definitely don't understand this.


Probably because the path names are modified later on and the implementer
of find wanted to avoide side effects on the arguments. Or he wanted to
make sure that modifications to the argument objects don't affect find()
as it is likely to run comparatively long compared to other methods.

Kind regards

robert

 
Reply With Quote
 
 
 
 
Csaba Henk
Guest
Posts: n/a
 
      01-26-2005
On 2005-01-26, Robert Klemme <(E-Mail Removed)> wrote:
>
> "Keith P. Boruff" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:89KJd.989$(E-Mail Removed)...
>> After reading about these two methods for the Object class in "Pragmatic
>> Programmer", I'm still having a hard time understanding the differences.
>> Can someone point me to some better info on these or explain it (I do
>> understand the differences between deep and shallow copying).

>
> They do both shallow copy.
>
> Here's a difference:
>
>>> s="foo".freeze

>=> "foo"
>>> s.frozen?

>=> true
>>> s.clone.frozen?

>=> true
>>> s.dup.frozen?

>=> false


Or:

irb(main):189:0> o=Object.new
=> #<Object:0x414c1f54>
irb(main):190:0> def o.foo; end
=> nil
irb(main):191:0> o2=o.clone
=> #<Object:0x414be1b0>
irb(main):192:0> o2.foo
=> nil
irb(main):193:0> o3=o.dup
=> #<Object:0x414baee8>
irb(main):194:0> o3.foo
NameError: undefined method `foo' for #<Object:0x414baee8>
from (irb):194
from /usr/lib/ruby/1.8/yaml/rubytypes.rb:67

Csaba
 
Reply With Quote
 
Keith P. Boruff
Guest
Posts: n/a
 
      01-26-2005
Robert Klemme wrote:

> They do both shallow copy.
>
> Here's a difference:
>
>>> s="foo".freeze

> => "foo"
>>> s.frozen?

> => true
>>> s.clone.frozen?

> => true
>>> s.dup.frozen?

> => false


So it looks like the major difference is that clone copies the state of the
original object and dup doesn't? At least in the context of "freeze"?

> Probably because the path names are modified later on and the implementer
> of find wanted to avoide side effects on the arguments. Or he wanted to
> make sure that modifications to the argument objects don't affect find()
> as it is likely to run comparatively long compared to other methods.


Got it! Thanks!

KPB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rubyzip - `dup': can't dup NilClass (TypeError) Luka Stolyarov Ruby 10 09-11-2010 12:13 PM
:s.respond_to?(:dup) && :s.dup raises Fran├žois Beausoleil Ruby 1 04-05-2007 05:55 PM
dup and clone Raj Sahae Ruby 4 03-01-2007 11:23 PM
Why do #dup and #clone behave differently with respect to instance methods Robert Klemme Ruby 5 05-18-2004 09:21 AM
[newbie] What is the difference between dup and clone ? Alexey Verkhovsky Ruby 1 04-03-2004 10:04 PM



Advertisments