Sha256: cc8b9066f99267e9cb4709127b187cf252e09e10ffd0e5745beada34c5a9f4da

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

class Object
  # mainly from active_support on http://github.com/rails/rails/

  # 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']
  def returning(value)
    yield(value)
    value
  end

  # Tries to send the method only if object responds to it. Return +nil+ otherwise.
  #
  # ==== Example :
  #
  # # Without try
  # @person ? @person.name : nil
  #
  # With try
  # @person.try(:name)
  def try(method, *args, &block)
    send(method, *args, &block) if respond_to?(method, true)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cloud_connect-2.0.2 lib/ext/object.rb
cloud_connect-2.0.1 lib/ext/object.rb
cloud_connect-2.0.0 lib/ext/object.rb
cloud_connect-0.0.2 lib/ext/object.rb
cloud_connect-0.0.1 lib/ext/object.rb