class Object

Public Instance Methods

class_def(name, &block) click to toggle source

whytheluckystiff: whytheluckystiff.net/articles/seeingMetaclassesClearly.html

Adds a class instance method.

Example:

SomeClass.class_def(:whoami) { 'I am SomeClass, silly!' }

SomeClass.whoami #=> "I am SomeClass, silly!"

Returns: A Proc object of the method, or nil.

# File lib/extra_lib/core_ext/object.rb, line 54
def class_def(name, &block)
        class_eval { define_method(name, &block) } if kind_of? Class
end
deepcopy() click to toggle source

Credit to the original author. This method retrieves a deep copy of the current object.

Returns: Deep copy of the same object.

# File lib/extra_lib/core_ext/object.rb, line 63
def deepcopy
        Marshal.load(Marshal.dump(self))
end
meta_def(name, &block) click to toggle source

whytheluckystiff: whytheluckystiff.net/articles/seeingMetaclassesClearly.html

Define an instance method on the metaclass.

Example: s = 'foo'; s.meta_def(:longer) { self * 2 }; s.longer #=> "foofoo"

Returns: A Proc object of the method.

# File lib/extra_lib/core_ext/object.rb, line 38
def meta_def(name, &block)
        meta_eval { define_method(name, &block) }
end
meta_eval(&block) click to toggle source

whytheluckystiff: whytheluckystiff.net/articles/seeingMetaclassesClearly.html

Evaluate code on the metaclass.

Example:

s = 'foo'; s.meta_eval { define_method(:longer) { self * 2 } }

s.longer #=> "foofoo"'

Returns: The block’s final expression.

# File lib/extra_lib/core_ext/object.rb, line 26
def meta_eval(&block)
        metaclass.instance_eval(&block)
end
metaclass() click to toggle source

whytheluckystiff: whytheluckystiff.net/articles/seeingMetaclassesClearly.html

Gets a metaclass (a class of a class).

Example: 'hello'.metaclass #=> #<Class:#<String:0xb7a57998>>

Returns: The metaclass.

# File lib/extra_lib/core_ext/object.rb, line 10
def metaclass
        class << self; self; end
end
to_bool() click to toggle source

Convert object to boolean.

Example:

"foo".to_bool #=> true

false.to_bool #=> false

nil.to_bool #=> nil

true.to_bool #=> true

Returns: Boolean or nil.

# File lib/extra_lib/core_ext/object.rb, line 81
def to_bool
        if [FalseClass, NilClass].include? self.class
                self
        else
                true
        end
end