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

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?

:nodoc



13
14
15
# File 'lib/support/active_support_lite/blank.rb', line 13

def blank?
  respond_to?(:empty?) ? empty? : !self
end

#extended_byObject

: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

#present?Boolean

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

#returning(value) {|value| ... } ⇒ Object

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']

Yields:

  • (value)


39
40
41
42
# File 'lib/support/active_support_lite/misc.rb', line 39

def returning(value)
  yield(value)
  value
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



55
56
57
58
# File 'lib/support/active_support_lite/misc.rb', line 55

def tap
  yield self
  self
end