dup vs clone 
Author Message
 dup vs clone

I spent some time this weekend purusing the "Programming Ruby" book
(Great book, btw).  As is common, more knowledge leads to more
questions.  So here is the first of several ...

The "Programming Ruby" book indicates that there may be semantic
differences between "dup" and "clone", particularly in descendent
classes.  I'm not entirely sure why there would be a difference and
exactly what that difference is.  Could someone elaborate?

--

---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



Sat, 03 May 2003 07:24:32 GMT  
 dup vs clone

Quote:
> The "Programming Ruby" book indicates that there may be semantic
> differences between "dup" and "clone", particularly in descendent
> classes.  I'm not entirely sure why there would be a difference and
> exactly what that difference is.  Could someone elaborate?

Jim,

I'd love to be able to give you definitive answer but I'm not. After some
quick source reading I have to say I'm more puzzled. Anyway the idea is
quite well stated at

  http://www.rubycentral.com/ref/ref_c_object.html#dup

and

  http://www.rubycentral.com/ref/ref_c_object.html#clone

So, the basic idea is that clone works like "memcpy", copying the whole
state in which the object was (including instance variables, taintedness and
other flags), while dup concentrates only to copy the "contents", or
payload, of the object.

Anyway, here's an example:

  # Couple of dup and clone differences

  a = []
  a.freeze
  b = a.clone
  c = a.dup
  p a.frozen?, b.frozen?, c.frozen?
    # => true, true, false

  class Ary < Array
    def foo

    end
    def inspect

    end
  end

  a = Ary.new
  a.foo
  b = a.clone
  c = a.dup
  p a.inspect, b.inspect, c.inspect
    # => "foo []", "foo []", " []"

(I'm not sure Ary.dup (which is inherited from Array) should _not_ copy
instance variables. I'm not sure enough of the concepts, but it might be a
design flaw (or, in other words, a bug :)).

        - Aleksi



Sat, 03 May 2003 08:09:03 GMT  
 dup vs clone

Quote:

> The "Programming Ruby" book indicates that there may be semantic
> differences between "dup" and "clone", particularly in descendent
> classes.  I'm not entirely sure why there would be a difference and
> exactly what that difference is.  Could someone elaborate?

dup is used to copy the contents of an object, but not necessarily all
its internal attributes. You can think of dup as a kind of copy
contructor.

clone on the other hand tries to copy internal state as well.

For example:

     a = "hello"      # => "hello"
     a.freeze           # => "hello"

     b = a.dup          # => "hello"
     b.frozen?          # => false

     c = a.clone        # => "hello"
     c.frozen?          # => true

Quote:
> 1) What's the best way to convert from a class name string to a class
>    object.  I've been using Eval(class_name) and trapping any NameErrors.
>    I could also search ObjectSpace looking for a match.  Is there a
>    better way?

Eval seems reasonable. If you _know_ that the name is a class, you
could also use Object.const_get(string)

   Object.const_get("Array")          # => Array

Quote:
> 3) I would like to be able to tie classes and methods to source
>    files.  Currently, I don't see a direct way of doing this.  The
>    "Programming Ruby" book hints at using runtime callbacks to capture
>    information about the system being defined.  I'm thinking about
>    wrapping "load" and "require" to record the file currently being
>    loaded and then hook into class and method definitions to record
>    that information.  Does this sound reasonable?

You'll need to remember to remove the file names from $" if you're
using require. You can also get the file name from set_trace_func it
you can find a safe method to call.

Quote:
> 4) What about reloading source files?  I would like to be able to
>    reload a source file to get the latest changes.  I suspect that the
>    naive approach might have problems.  Can source files be arbitrarily
>    reloaded?

My guess is it would depend on the class. If a class sets global
state, then it might be tricky.

Quote:
> How can I detect deleted methods?

You could hook Module.remove_method and .undef_method.

I'd personally like to see more browser support in 1.7, which is why I
started the browser thread earlier. Your experiments are leading the
way to defining stuff that we might like (particularly if you start
working on compile and go and incremental compilation ;-)

Regards

Dave



Sat, 03 May 2003 08:28:16 GMT  
 dup vs clone
Hi,

In message "[ruby-talk:6323] dup vs clone"

|The "Programming Ruby" book indicates that there may be semantic
|differences between "dup" and "clone", particularly in descendent
|classes.  I'm not entirely sure why there would be a difference and
|exactly what that difference is.  Could someone elaborate?

`clone' copies everything; internal state, singleton methods, etc.
`dup' copies object contents only (plus taintness status).

                                                        matz.



Sat, 03 May 2003 08:24:17 GMT  
 dup vs clone

Quote:
> Eval seems reasonable. If you _know_ that the name is a class, you
> could also use Object.const_get(string)

>    Object.const_get("Array")             # => Array

Eval is probably good. But the simple way doesn't always work. The problem
is the classes which are inside a name space:

  module Foo
    class Bar
    end
  end

  def find(klass,name)
    begin
      k = klass.const_get(name)
      puts "Found #{name} from #{klass}, so it is #{k}"
    rescue NameError
      puts "Did not find #{name} from #{klass}"
    end
  end

  [Object, Module, Module.const_get("Foo")].each do |klass|
    ["Foo", "Bar"].each do |name|
      find(klass, name)
    end
    puts
  end

Outputting:

  Found Foo from Object, so it is Foo
  Did not find Bar from Object

  Found Foo from Module, so it is Foo
  Did not find Bar from Module

  Found Foo from Foo, so it is Foo
  Found Bar from Foo, so it is Foo::Bar

        - Aleksi



Sat, 03 May 2003 09:16:35 GMT  
 dup vs clone
If

    VALUE tdata =  Data_Make_Struct(class,c-type,mark,free,ptr)

is called but

    rb_obj_call_init(tdata,0,0)

never is will the garbage collector be called on tdata?

My concern is that free might never be called with ptr.

John



Sat, 03 May 2003 09:28:58 GMT  
 dup vs clone

    >> 4) What about reloading source files?  [...]
    >> How can I detect deleted methods?

    Dave> You could hook Module.remove_method and .undef_method.

Oops.  I wasn't clear.  I was thinking out load about the problems of
detecting methods deleted from source files.  Reloading the source
file will leave the original method definition unmodified.

    Dave> I'd personally like to see more browser support in 1.7,

Agreed.  We just need to figure out what we need.

--

---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



Sat, 03 May 2003 10:04:32 GMT  
 dup vs clone
Hi,

In message "[ruby-talk:6333] If Data_Make_Struct(class,c-type,mark,free,ptr) is called but"

|
|    VALUE tdata =  Data_Make_Struct(class,c-type,mark,free,ptr)
|
|is called but
|
|    rb_obj_call_init(tdata,0,0)
|
|never is will the garbage collector be called on tdata?

rb_obj_call_init() has no relation with freeing ptr.   It just call
`initialize' method for newly created object.

                                                        matz.



Sat, 03 May 2003 13:00:44 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. dup, clone, and "deep copy"

2. stdcall vs c vs cdecl vs pascal vs whosyerdaddy

3. 68K vs CFM68K vs Tk 8.0.3 vs AppearanceLib vs System 7

4. +R as a name for DUP >R

5. UNDROP an alternative to ?DUP

6. DUP >R

7. dup

8. Memory behavior of String#dup

9. self being altered even when .dup'ed

10. 5 dup ($)

11. Resb and dup in NASM

12. Problem with DUP

 

 
Powered by phpBB® Forum Software