### Home Classes Methods

### In Files

- lib/facets/object/replace.rb
- lib/facets/object/object_state.rb
- lib/facets/object/dup.rb
- lib/facets/applique/file_helpers.rb
- lib/facets/enumerator.rb
- lib/facets/enumerator/fx.rb
- lib/facets/kernel/instance_exec.rb

### Parent

BasicObject

### Methods

- #clone?
- #dup!
- #dup?
- #instance_exec
- #object_state
- #replace
- #temporary_directory
- #try_dup

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Lazy
- Lazy::Future
- Lazy::Promise
- Lazy::Promise::DIVERGES
- Lazy::PromiseSafe
- Enumerable
- Enumerable::Recursor
- Method
- Method::Composition
- Module
- Module::InstanceFunction
- Array
- Binding
- Class
- Comparable
- Denumerable
- Denumerator
- Dir
- Enumerator
- Exception
- Facets
- FalseClass
- File
- FileTest
- Fixnum
- Float
- Functor
- Hash
- Indexable
- Instance
- Integer
- Kernel
- MatchData
- NA
- NilClass
- Numeric
- Object
- ObjectSpace
- Proc
- Process
- Range
- Regexp
- String
- Struct
- Symbol
- Time
- TrueClass
- UnboundMethod

No matching classes.

# Object

### Constants

Enumerator

  for Ruby 1.8 -> 1.9 transition

### Public Instance Methods

clone?() click to toggle source

    # File lib/facets/object/dup.rb, line 23
    def clone? ; true ; end

dup!() click to toggle source

Override this in a child class if it cannot be dup'ed.

    obj1 = Object.new
    obj2 = obj1.dup!
    obj2.equal?(obj1)    #=> false

CREDIT: Dan Kubb (extlib)

    # File lib/facets/object/dup.rb, line 9
    def dup!
      dup
    end

dup?() click to toggle source

Can you safely call dup on this object?

Returns `false` for `nil`, `false`, `true`, symbols, and numbers; `true` otherwise.

    # File lib/facets/object/dup.rb, line 22
    def dup?   ; true ; end

instance_exec(*args, &block) click to toggle source

Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.

From Mauricio's eigenclass.org/hiki/bounded+space+instance_exec

This version has been borrowed from Rails for compatibility sake.

NOTE: This is not a common core extension (due to the use of thread.rb) and is not loaded automatically when using `require 'facets'`. However it is a core method in Ruby 1.9, so this only matters for users of Ruby 1.8.x or below.

@uncommon

    require 'facets/kernel/instance_exec'

    # File lib/facets/kernel/instance_exec.rb, line 27
    def instance_exec(*args, &block)
      begin
        old_critical, Thread.critical = Thread.critical, true
        n = 0
        n += 1 while respond_to?(method_name = "__instance_exec#{n}")
        InstanceExecMethods.module_eval { define_method(method_name, &block) }
      ensure
        Thread.critical = old_critical
      end

      begin
        __send__(method_name, *args)
      ensure
        InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
      end
    end

object_state(data=nil) click to toggle source

Get or set state of object. You can think of object_state as an in-code form of marshalling.

    class StateExample
      attr_reader :a, :b
      def initialize(a,b)
        @a, @b = a, b
      end
    end

    obj = StateExample.new(1,2)
    obj.a  #=> 1
    obj.b  #=> 2

    obj.object_state  #=> {:a=>1, :b=>2}

    obj.object_state(:a=>3, :b=>4)
    obj.a  #=> 3
    obj.b  #=> 4

For most object's this is essentially the same as `instance.to_h`. But for data structures like Array and Hash it returns a snapshot of their contents, not the state of their instance variables.

    # File lib/facets/object/object_state.rb, line 28
    def object_state(data=nil)
      if data
        instance_variables.each do |iv|
          name = iv.to_s.sub(/^[@]/, '').to_sym
          instance_variable_set(iv, data[name])
        end
      else
        data = {}
        instance_variables.each do |iv|
          name = iv.to_s.sub(/^[@]/, '').to_sym
          data[name] = instance_variable_get(iv)
        end
        data
      end
    end

replace(source) click to toggle source

Replace state of object with the state of another object of the same class (or superclass).

    class ReplaceExample
      attr_reader :a, :b
      def initialize(a,b)
        @a, @b = a, b
      end
    end

    obj1 = ReplaceExample.new(1,2)
    obj1.a  #=> 1
    obj1.b  #=> 2

    obj2 = ReplaceExample.new(3,4)
    obj2.a  #=> 3
    obj2.b  #=> 4

    obj1.replace(obj2)
    obj1.a  #=> 3
    obj1.b  #=> 4

This is very similar to `instance.update`, but it is limited by the class of objects, in the same manner as Array#replace.

    # File lib/facets/object/replace.rb, line 27
    def replace(source)
      raise ArgumentError, "not a #{self.class} -- #{source}" unless source.is_a?(self.class)
      instance_variables.each do |iv|
        instance_variable_set(iv, source.instance_variable_get(iv))
      end
    end

temporary_directory() click to toggle source

    # File lib/facets/applique/file_helpers.rb, line 2
    def temporary_directory
      @temporary_directory ||= 'tmp'
    end

try_dup() click to toggle source

Alternative name for dup!

    # File lib/facets/object/dup.rb, line 14
    def try_dup
      dup!
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.