Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/tinyext/core/tap.rb,
lib/tinyext/core/try.rb,
lib/tinyext/core/blank.rb
Overview
TAKEN FROM ACTIVE SUPPORT
Instance Method Summary collapse
-
#blank? ⇒ Boolean
An object is blank if it’s false, empty, or a whitespace string.
-
#present? ⇒ Boolean
An object is present if it’s not blank.
-
#tap {|_self| ... } ⇒ Object
Yields
xto the block, and then returnsx.
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?
13 14 15 |
# File 'lib/tinyext/core/blank.rb', line 13 def blank? respond_to?(:empty?) ? empty? : !self end |
#present? ⇒ Boolean
An object is present if it’s not blank.
18 19 20 |
# File 'lib/tinyext/core/blank.rb', line 18 def present? !blank? end |
#tap {|_self| ... } ⇒ Object
Yields x to the block, and then returns x. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.
(1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
tap { |x| puts "array: #{x.inspect}" }.
select { |x| x%2 == 0 }.
tap { |x| puts "evens: #{x.inspect}" }.
map { |x| x*x }.
tap { |x| puts "squares: #{x.inspect}" }
13 14 15 16 |
# File 'lib/tinyext/core/tap.rb', line 13 def tap yield self self end |