Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/support/active_support_lite/misc.rb,
lib/support/core_ext.rb,
lib/support/active_support_lite/blank.rb,
lib/support/active_support_lite/object.rb
Overview
:nodoc:all
Instance Method Summary (collapse)
-
- (Boolean) blank?
An object is blank if it's false, empty, or a whitespace string.
-
- (Object) extended_by
:nodoc.
-
- (Boolean) present?
An object is present if it's not blank.
-
- (Object) returning(value) {|value| ... }
Returns value after yielding value to the block.
- - (Object) tap {|_self| ... }
Instance Method Details
- (Boolean) blank?
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?
:nodoc
13 14 15 |
# File 'lib/support/active_support_lite/blank.rb', line 13 def blank? respond_to?(:empty?) ? empty? : !self end |
- (Object) extended_by
:nodoc
4 5 6 7 |
# File 'lib/support/active_support_lite/object.rb', line 4 def extended_by #:nodoc ancestors = class << self; ancestors end ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ] end |
- (Boolean) present?
An object is present if it's not blank.
19 20 21 |
# File 'lib/support/active_support_lite/blank.rb', line 19 def present? !blank? end |
- (Object) returning(value) {|value| ... }
Returns value after yielding value to the block. This simplifies the process of constructing an object, performing work on the object, and then returning the object from a method. It is a Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
Examples
# Without returning
def foo
values = []
values << "bar"
values << "baz"
return values
end
foo # => ['bar', 'baz']
# returning with a local variable
def foo
returning values = [] do
values << 'bar'
values << 'baz'
end
end
foo # => ['bar', 'baz']
# returning with a block argument
def foo
returning [] do |values|
values << 'bar'
values << 'baz'
end
end
foo # => ['bar', 'baz']
39 40 41 42 |
# File 'lib/support/active_support_lite/misc.rb', line 39 def returning(value) yield(value) value end |
- (Object) tap {|_self| ... }
55 56 57 58 |
# File 'lib/support/active_support_lite/misc.rb', line 55 def tap yield self self end |