[セットトップ]rubyオブジェクトをコピーする方法(dupとclone)

3629 ワード

Ruby     Object#clone Object#dup    copy    ,     dup        , clone            , singleton method

[ruby] view plaincopyprint?

s = "cat"  

def s.upcase  

    "CaT"  

end  

s_dup = s.dup  

s_clone = s.clone  

s_dup.upcase        #=> "CAT"  (singleton method not copied)  

s_clone.upcase      #=> "CaT" (uses singleton method)  



dup clone     Shallow Copy,             ,                ,             。

[ruby] view plaincopyprint?

arr1 = [ 1, "flipper", 3 ]  

arr2 = arr1.dup  

arr2[2] = 99  

arr2[1][2] = 'a'  

arr1                #=> [1, "flapper", 3]  

arr2                #=> [1, "flapper", 99]  

    arr1     String     ,  arr2        , arr2        ,arr1        。

        Deep Copy,       Marshal  

[ruby] view plaincopyprint?

arr1 = [ 1, "flipper", 3 ]  

arr2 = Marshal.load(Marshal.dump(arr1))  

arr2[2] = 99  

arr2[1][2] = 'a'  

arr1                #=> [1, "flipper", 3]  

arr2                #=> [1, "flapper", 99]  

      arr2  String         arr1    ,    。。。  Marshal               

 class                 initialize_copy,               ,       

[ruby] view plaincopyprint?

class Document  

    attr_accessor :title, :text  

    attr_reader :timestamp  

  

    def initialize(title, text)  

        @title, @text = title, text  

        @timestamp = Time.now  

    end  

end  

  

doc1 = Document.new("Random Stuff", "Haha")  

sleep 10  

doc2 = doc1.clone  

  

doc1.timestamp == doc2.timestamp        #=> true  

             ,    initialize    ,          timestamp    。               ,       Document   initialize_copy     。initialize_copy                

[ruby] view plaincopyprint?

class Document    #Reopen the class  

    def initialize_copy(other)  

        @timestamp = Time.now  

    end  

end  

  

doc3 = Document.new("More Stuff", "Haha")  

sleep 10  

doc4 = doc1.clone  

  

doc3.timestamp == doc4.timestamp        #=> false  

    Ruby   。。。

PS:        The Ruby Way



 Ruby      (object)            .    google   ,      .

        , b = a     ?      :

>> a= [0,[1,2]]

>> b=a

>> b[0]=88

>> b[1][0]=99

>> b  

=> [88, [99, 2]]

>> a  

=> [88, [99, 2]]

       ,     b,    a       .   :



>> b.equal?(a)

=> true

  b a       object,          .   b = a    .

  b = a.dup ??      :

>> a= [0,[1,2]]

>> b=a.dup

>> b[0]=88

>> b[1][0]=99

>> b

=> [88, [99, 2]]

>> a

=> [0, [99, 2]]

        ,    b , a          .(0   ,    1   99).

  dup      (  Array     ),         .

    , b = a.clone ?    :

>> a= [0,[1,2]]

>> b=a.clone

>> b[0]=88

>> b[1][0]=99

>> b

=> [88, [99, 2]]

>> a

=> [0, [99, 2]]

     dup    .   clone         ! 

  ruby  dup clone  shallow  ,    object      . 

 ,    Ruby          ?      ,    :

>> a= [0,[1,2]]

>> b=Marshal.load(Marshal.dump(a))

>> b[0]=88

>> b[1][0]=99

>> b

=> [88, [99, 2]]

>> a= [0,[1,2]]

=> [0, [1, 2]]

  b a     !!!               !!!

     "  " ?     object    Marshal.dump . :

>> t=Object.new

>> def t.test; puts ‘test’ end

>> Marshal.dump(t)

TypeError: singleton can’t be dumped

    from (irb):59:in `dump’

    from (irb):59

             ruby    deep clone  ,         :

http://d.hatena.ne.jp/pegacorn/20070417/1176817721

http://www.artima.com/forums/flat.jsp?forum=123&thread=40913