Class: Object
- Inherits:
- BasicObject
- Extended by:
- Hoodie::Konstruktor
- Defined in:
- lib/hoodie/core_ext/try.rb,
lib/hoodie/core_ext/blank.rb,
lib/hoodie/utils/konstruktor.rb
Overview
Add #blank? and #present? methods to Object class.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Returns true if the object is nil or empty (if applicable).
-
#present? ⇒ Boolean
Returns true if the object is NOT nil or empty.
-
#try(*a, &b) ⇒ Object
Invokes the public method whose name goes as first argument just like
public_senddoes, except that if the receiver does not respond to it the call returnsnilrather than raising an exception. -
#try!(*a, &b) ⇒ Object
Same as #try, but will raise a NoMethodError exception if the receiver is not
niland does not implement the tried method.
Methods included from Hoodie::Konstruktor
Instance Method Details
#blank? ⇒ Boolean
Returns true if the object is nil or empty (if applicable)
[].blank? #=> true
[1].blank? #=> false
[nil].blank? #=> false
30 31 32 |
# File 'lib/hoodie/core_ext/blank.rb', line 30 def blank? nil? || (respond_to?(:empty?) && empty?) end |
#present? ⇒ Boolean
Returns true if the object is NOT nil or empty
[].present? #=> false
[1].present? #=> true
[nil].present? #=> true
42 43 44 |
# File 'lib/hoodie/core_ext/blank.rb', line 42 def present? !blank? end |
#try(*a, &b) ⇒ Object
try is defined on Object. Therefore, it won’t work with instances
Invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.
of classes that do not have Object among their ancestors, like direct subclasses of BasicObject.
35 36 37 |
# File 'lib/hoodie/core_ext/try.rb', line 35 def try(*a, &b) try!(*a, &b) if a.empty? || respond_to?(a.first) end |
#try!(*a, &b) ⇒ Object
Same as #try, but will raise a NoMethodError exception if the receiver is not nil and does not implement the tried method.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/hoodie/core_ext/try.rb', line 45 def try!(*a, &b) if a.empty? && block_given? if b.arity.zero? instance_eval(&b) else yield self end else public_send(*a, &b) end end |