[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dennou-ruby:000101] Re: deep copy



ごとけんです

In message "[dennou-ruby:000100] Re: deep copy"
    on 99/09/24, GOTO Kentaro <gotoken@xxxxxx> writes:
>module DeepCopieable
>  require "marshal"
>
>  def deep_copy
>    # this method uses marshal, so Class, Module, IO, Data and their
>    # descendants cannot be copied. 
>    r, w = IO.pipe
>    Marshal.dump(self, w)
>    w.close
>    Marshal.load(r)
>  end
>end

わざわざpipe使うこともないですね:

module DeepCopieable
  require "marshal"

  def deep_copy
    # this method uses marshal, so Class, Module, IO, Data and their
    # successors cannot be copy. 

    Marshal.load(Marshal.dump(self))
  end
end