Sha256: 2c413e6fb5f231a7b80a67c754d2343e8a929226f4ca1f900956b1cc692693db

Contents?: true

Size: 673 Bytes

Versions: 3

Compression:

Stored size: 673 Bytes

Contents

require 'facets/functor'

module Kernel

  # TODO: Should Kernel#respond be moved to functor gem?

  # Like #respond_to? but returns the result of the call
  # if it does indeed respond.
  #
  #   class RespondExample
  #     def f; "f"; end
  #   end
  #
  #   x = RespondExample.new
  #   x.respond(:f)  #=> "f"
  #   x.respond(:g)  #=> nil
  #
  # This method was known as #try until Rails defined #try
  # to be something more akin to #ergo.
  #
  # CREDIT: Chris Wanstrath

  def respond(sym=nil, *args, &blk)
    if sym
      return nil if not respond_to?(sym)
      __send__(sym, *args, &blk)
    else
      Functor.new(&method(:respond).to_proc)
    end
  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/kernel/respond.rb
facets-3.1.0 lib/core/facets/kernel/respond.rb
facets-3.0.0 lib/core/facets/kernel/respond.rb