Sha256: d6887b4292b052179ef18b615b667054d679703fe654e19007095fc9458f720f

Contents?: true

Size: 887 Bytes

Versions: 6

Compression:

Stored size: 887 Bytes

Contents

module Kernel

  unless method_defined?(:tap)  # 1.8.7+

    # The tap K-Combinator. This yields self -and- returns self.
    #
    # Note, Ruby 1.9+ does not appear to support the zero arity 
    # instance_eval option.
    #
    def tap(&b)
      if block_given?
        b.arity == 1 ? yield(self) : instance_eval(&b)
      end
      self
    end

    # == Future definition?
    #
    # This is a consideration for a future #tap using Functor:
    #
    #   require 'facets/functor'
    #
    #   def tap(&b)
    #     if block_given?
    #       b.arity == 1 ? yield(self) : instance_eval(&b)
    #       self
    #     else
    #       Functor.new{ |op, *args| self.send(op, *args); self }
    #     end
    #   end
    #
    # It would allow a single call, before returning the original.
    # However there are not very many useful things you can do with
    # that.

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/kernel/tap.rb
facets-2.8.3 lib/core/facets/kernel/tap.rb
facets-2.8.2 lib/core/facets/kernel/tap.rb
facets-2.8.1 lib/core/facets/kernel/tap.rb
facets-2.8.0 lib/core/facets/kernel/tap.rb
facets-2.7.0 lib/core/facets/kernel/tap.rb