### Home Classes Methods

### In Files

- lib/facets/cloneable.rb

### Methods

- #initialize_copy

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# Cloneable

Clonable provides a standard basis for adding deep cloneablity to a class via the standard dup and clone methods.

Cloneable was originally ported from Jim Weirich's Rake. The current version is the work of Ken Bloom.

CREDIT: Ken Bloom

### Public Instance Methods

initialize_copy(sibling) click to toggle source

    # File lib/facets/cloneable.rb, line 11
    def initialize_copy(sibling)
      #first duplicate my superclass' state. Note that if it's duplicating
      #instance variables, this will be overwritten, but this is important
      #because we could be dealing with a C extension with state hidden from
      #the Ruby interpreter
      super

      #we want to know if we're being dup'ed or clone'd, because we want to
      #preserve the state of our internals the same way our state is being
      #preserved. (If we can't figure it out, we'll just use #dup.)
      operation = (
        copy_call  = caller.find{|x| x !~ /'initialize_copy'/}
        copy_match = copy_call.match(/`(dup|clone)'/)
        copy_match ? copy_match[1] : :dup
      )

      sibling.instance_variables.each do |ivar|
        value = sibling.instance_variable_get(ivar)

        #set my instance variable to be a #dup or #clone
        #or my sibling, depending on what's happening to me right now
        instance_variable_set(ivar, value.send(operation))
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.