Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/extlib/blank.rb,
lib/extlib/object.rb,
lib/extlib/try_dup.rb
Instance Method Summary collapse
-
#blank? ⇒ TrueClass, FalseClass
Returns true if the object is nil or empty (if applicable).
-
#full_const_get(name) ⇒ Object
The constant corresponding to the name.
-
#full_const_set(name, value) ⇒ Object
The constant corresponding to the name.
-
#in?(arrayish, *more) ⇒ Boolean
True if the object is included in arrayish (+ more).
- #instance_variable_defined?(variable) ⇒ Boolean
-
#make_module(str) ⇒ nil
Defines module from a string name (e.g. Foo::Bar::Baz) If module already exists, no exception raised.
-
#meta_class ⇒ Class
Extracts the singleton class, so that metaprogramming can be done on it.
-
#quacks_like?(duck) ⇒ Boolean
True if the object quacks like duck.
-
#try_call(*args) ⇒ Object
If receiver is callable, calls it and returns result.
-
#try_dup ⇒ Object
Override this in a child if it cannot be dup’ed.
Instance Method Details
#blank? ⇒ TrueClass, FalseClass
Returns true if the object is nil or empty (if applicable)
[].blank? #=> true
[1].blank? #=> false
[nil].blank? #=> false
12 13 14 |
# File 'lib/extlib/blank.rb', line 12 def blank? nil? || (respond_to?(:empty?) && empty?) end |
#full_const_get(name) ⇒ Object
Returns The constant corresponding to the name.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/extlib/object.rb', line 67 def full_const_get(name) list = name.split("::") list.shift if list.first.blank? obj = self list.each do |x| # This is required because const_get tries to look for constants in the # ancestor chain, but we only want constants that are HERE obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x) end obj end |
#full_const_set(name, value) ⇒ Object
Returns The constant corresponding to the name.
83 84 85 86 87 88 89 90 |
# File 'lib/extlib/object.rb', line 83 def full_const_set(name, value) list = name.split("::") toplevel = list.first.blank? list.shift if toplevel last = list.pop obj = list.empty? ? Object : Object.full_const_get(list.join("::")) obj.const_set(last, value) if obj && !obj.const_defined?(last) end |
#in?(arrayish, *more) ⇒ Boolean
Returns True if the object is included in arrayish (+ more).
163 164 165 166 |
# File 'lib/extlib/object.rb', line 163 def in?(arrayish,*more) arrayish = more.unshift(arrayish) unless more.empty? arrayish.include?(self) end |
#instance_variable_defined?(variable) ⇒ Boolean
174 175 176 |
# File 'lib/extlib/object.rb', line 174 def instance_variable_defined?(variable) instance_variables.include?(variable.to_s) end |
#make_module(str) ⇒ nil
Defines module from a string name (e.g. Foo::Bar::Baz) If module already exists, no exception raised.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/extlib/object.rb', line 98 def make_module(str) mod = str.split("::") current_module = self mod.each do |x| unless current_module.const_defined?(x) current_module.class_eval "module #{x}; end", __FILE__, __LINE__ end current_module = current_module.const_get(x) end current_module end |
#meta_class ⇒ Class
Extracts the singleton class, so that metaprogramming can be done on it.
62 |
# File 'lib/extlib/object.rb', line 62 def () class << self; self end end |
#quacks_like?(duck) ⇒ Boolean
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.
Returns True if the object quacks like duck.
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/extlib/object.rb', line 122 def quacks_like?(duck) case duck when Symbol self.respond_to?(duck) when Class self.is_a?(duck) when Array duck.any? {|d| self.quacks_like?(d) } else false end end |
#try_call(*args) ⇒ Object
If receiver is callable, calls it and returns result. If not, just returns receiver itself
147 148 149 150 151 152 153 |
# File 'lib/extlib/object.rb', line 147 def try_call(*args) if self.respond_to?(:call) self.call(*args) else self end end |
#try_dup ⇒ Object
Override this in a child if it cannot be dup’ed
138 139 140 |
# File 'lib/extlib/object.rb', line 138 def try_dup self.dup end |