Sha256: 1c9aac6988ef107a212081fd20acbe3f9504eb9360302cb293fab2c5faed72e5

Contents?: true

Size: 907 Bytes

Versions: 7

Compression:

Stored size: 907 Bytes

Contents

require 'facets/functor'

module Kernel

  # 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
  #
  # or
  #
  #   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: Trans, Chris Wanstrath

  def respond(sym=nil, *args, &blk)
    if sym
      return nil if not respond_to?(sym)
      __send__(sym, *args, &blk)
    else
      ## TODO: use after 1.8.6 not supported anymore
      ##Functor.new do |op, *a, &b|
      ##  respond(op, *a, &b)
      ##end
      Functor.new(&method(:respond).to_proc)
    end
  end

  # DEPRECATED: #respond is enough.
  #alias_method :respond_with_value, :respond

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/kernel/respond.rb
facets-2.9.2 src/core/facets/kernel/respond.rb
facets-2.9.2 lib/core/facets/kernel/respond.rb
facets-2.9.1 lib/core/facets/kernel/respond.rb
facets-2.9.0 lib/core/facets/kernel/respond.rb
facets-2.9.0.pre.2 lib/core/facets/kernel/respond.rb
facets-2.9.0.pre.1 lib/core/facets/kernel/respond.rb