Sha256: 5696be3df709328b2e1fd000fe1a65f69b66f9c69364a9a18411675b8de97db9

Contents?: true

Size: 754 Bytes

Versions: 3

Compression:

Stored size: 754 Bytes

Contents

class Object
  # The method #as allows you to take an object and use it in a block. If
  # you return a non-nil value, #as will return that value, otherwise the
  # original object is returned.
  #
  # This is particularly useful with super.
  #
  # ```ruby
  # def overridden_method
  #   super.as |value|
  #     if value.is_a? String
  #       Array.wrap(value)
  #     end
  #   end
  # end
  # ```
  #
  # Is equivalent to:
  #
  # ```ruby
  # def overridden_method
  #   _overridden_method = super
  #
  #   if _overridden_method.is_a? String
  #     Array.wrap(_overridden_method)
  #   else
  #     _overridden_method
  #   end
  # end
  # ```
  def _as
    result = yield(self)
    result.nil? ? self : result
  end

  alias_method :as, :_as
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tfg_support-1.1.1 lib/tfg/support/core_ext/object/as.rb
tfg_support-1.0.1 lib/tfg/support/core_ext/object/as.rb
tfg_support-1.0.0 lib/tfg/support/core_ext/object/as.rb