Class Object
In: lib/extlib/blank.rb
lib/extlib/object.rb
Parent: Object
dot/f_30.png

Methods

Public Instance methods

Returns true if the object is nil or empty (if applicable)

  [].blank?         #=>  true
  [1].blank?        #=>  false
  [nil].blank?      #=>  false

@return [TrueClass, FalseClass]

@api public

[Source]

    # File lib/extlib/blank.rb, line 12
12:   def blank?
13:     nil? || (respond_to?(:empty?) && empty?)
14:   end

@param name<String> The name of the constant to get, e.g. "Merb::Router".

@return <Object> The constant corresponding to the name.

[Source]

    # File lib/extlib/object.rb, line 64
64:   def full_const_get(name)
65:     list = name.split("::")
66:     list.shift if list.first.blank?
67:     obj = self
68:     list.each do |x|
69:       # This is required because const_get tries to look for constants in the
70:       # ancestor chain, but we only want constants that are HERE
71:       obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
72:     end
73:     obj
74:   end

@param name<String> The name of the constant to get, e.g. "Merb::Router". @param value<Object> The value to assign to the constant.

@return <Object> The constant corresponding to the name.

[Source]

    # File lib/extlib/object.rb, line 80
80:   def full_const_set(name, value)
81:     list = name.split("::")
82:     toplevel = list.first.blank?
83:     list.shift if toplevel
84:     last = list.pop
85:     obj = list.empty? ? Object : Object.full_const_get(list.join("::"))
86:     obj.const_set(last, value) if obj && !obj.const_defined?(last)
87:   end

@param arrayish<include?> Container to check, to see if it includes the object.

@param *more<Array>:additional args, will be flattened into arrayish

@return <TrueClass, FalseClass>

  True if the object is included in arrayish (+ more)

@example 1.in?([1,2,3]) #=> true @example 1.in?(1,2,3) #=> true

[Source]

     # File lib/extlib/object.rb, line 160
160:   def in?(arrayish,*more)
161:     arrayish = more.unshift(arrayish) unless more.empty?
162:     arrayish.include?(self)
163:   end

[Source]

     # File lib/extlib/object.rb, line 171
171:     def instance_variable_defined?(variable)
172:       instance_variables.include?(variable.to_s)
173:     end

Defines module from a string name (e.g. Foo::Bar::Baz) If module already exists, no exception raised.

@param name<String> The name of the full module name to make

@return <NilClass>

[Source]

     # File lib/extlib/object.rb, line 95
 95:   def make_module(str)
 96:     mod = str.split("::")
 97:     current_module = self
 98:     mod.each do |x|
 99:       unless current_module.const_defined?(x)
100:         current_module.class_eval "module #{x}; end", __FILE__, __LINE__
101:       end
102:       current_module = current_module.const_get(x)
103:     end
104:     current_module
105:   end

Extracts the singleton class, so that metaprogramming can be done on it.

@return <Class> The meta class.

@example [Setup]

  class MyString < String; end

  MyString.instance_eval do
    define_method :foo do
      puts self
    end
  end

  MyString.meta_class.instance_eval do
    define_method :bar do
      puts self
    end
  end

  def String.add_meta_var(var)
    self.meta_class.instance_eval do
      define_method var do
        puts "HELLO"
      end
    end
  end

@example

  MyString.new("Hello").foo #=> "Hello"

@example

  MyString.new("Hello").bar
    #=> NoMethodError: undefined method `bar' for "Hello":MyString

@example

  MyString.foo
    #=> NoMethodError: undefined method `foo' for MyString:Class

@example

  MyString.bar
    #=> MyString

@example

  String.bar
    #=> NoMethodError: undefined method `bar' for String:Class

@example

  MyString.add_meta_var(:x)
  MyString.x #=> HELLO

@details [Description of Examples]

  As you can see, using #meta_class allows you to execute code (and here,
  define a method) on the metaclass itself. It also allows you to define
  class methods that can be run on subclasses, and then be able to execute
  code on the metaclass of the subclass (here MyString).

  In this case, we were able to define a class method (add_meta_var) on
  String that was executable by the MyString subclass. It was then able to
  define a method on the subclass by adding it to the MyString metaclass.

  For more information, you can check out _why's excellent article at:
  http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html

[Source]

    # File lib/extlib/object.rb, line 59
59:   def meta_class() class << self; self end end

@param duck<Symbol, Class, Array> The thing to compare the object to.

@note

  The behavior of the method depends on the type of duck as follows:
  Symbol:: Check whether the object respond_to?(duck).
  Class:: Check whether the object is_a?(duck).
  Array::
    Check whether the object quacks_like? at least one of the options in the
    array.

@return <TrueClass, FalseClass>

  True if the object quacks like duck.

[Source]

     # File lib/extlib/object.rb, line 119
119:   def quacks_like?(duck)
120:     case duck
121:     when Symbol
122:       self.respond_to?(duck)
123:     when Class
124:       self.is_a?(duck)
125:     when Array
126:       duck.any? {|d| self.quacks_like?(d) }
127:     else
128:       false
129:     end
130:   end

If receiver is callable, calls it and returns result. If not, just returns receiver itself

@return <Object>

[Source]

     # File lib/extlib/object.rb, line 144
144:   def try_call(*args)
145:     if self.respond_to?(:call)
146:       self.call(*args)
147:     else
148:       self
149:     end
150:   end

Override this in a child if it cannot be dup‘ed

@return <Object>

[Source]

     # File lib/extlib/object.rb, line 135
135:   def try_dup
136:     self.dup
137:   end

[Validate]