Class: Object
- Inherits:
- BasicObject
- Includes:
- InstanceExecMethods
- Defined in:
- lib/ant_support/core_ext/blank.rb,
lib/ant_support/core_ext/duplicable.rb,
lib/ant_support/core_ext/object/extending.rb
Defined Under Namespace
Modules: InstanceExecMethods
Instance Method Summary collapse
-
#blank? ⇒ Boolean
An object is blank if it’s false, empty, or a whitespace string.
-
#duplicable? ⇒ Boolean
Can you safely .dup this object? False for nil, false, true, symbols, and numbers; true otherwise.
-
#extend_with_included_modules_from(object) ⇒ Object
:nodoc:.
-
#extended_by ⇒ Object
:nodoc:.
-
#instance_exec(*args, &block) ⇒ Object
Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.
-
#present? ⇒ Boolean
An object is present if it’s not blank.
-
#remove_subclasses_of(*superclasses) ⇒ Object
:nodoc:.
Instance Method Details
#blank? ⇒ Boolean
An object is blank if it’s false, empty, or a whitespace string. For example, “”, “ ”, nil
, [], and {} are blank.
This simplifies
if !address.nil? && !address.empty?
to
if !address.blank?
12 13 14 |
# File 'lib/ant_support/core_ext/blank.rb', line 12 def blank? respond_to?(:empty?) ? empty? : !self end |
#duplicable? ⇒ Boolean
Can you safely .dup this object? False for nil, false, true, symbols, and numbers; true otherwise.
4 5 6 |
# File 'lib/ant_support/core_ext/duplicable.rb', line 4 def duplicable? true end |
#extend_with_included_modules_from(object) ⇒ Object
:nodoc:
50 51 52 |
# File 'lib/ant_support/core_ext/object/extending.rb', line 50 def extend_with_included_modules_from(object) #:nodoc: object.extended_by.each { |mod| extend mod } end |
#extended_by ⇒ Object
:nodoc:
45 46 47 48 |
# File 'lib/ant_support/core_ext/object/extending.rb', line 45 def extended_by #:nodoc: ancestors = class << self; ancestors end ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ] end |
#instance_exec(*args, &block) ⇒ Object
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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ant_support/core_ext/object/extending.rb', line 63 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 |
#present? ⇒ Boolean
An object is present if it’s not blank.
17 18 19 |
# File 'lib/ant_support/core_ext/blank.rb', line 17 def present? !blank? end |
#remove_subclasses_of(*superclasses) ⇒ Object
:nodoc:
2 3 4 |
# File 'lib/ant_support/core_ext/object/extending.rb', line 2 def remove_subclasses_of(*superclasses) #:nodoc: Class.remove_class(*subclasses_of(*superclasses)) end |