Method: Kernel#respond

Defined in:
lib/garcon/core_ext/kernel.rb

#respond(sym = nil, *args, &blk) ⇒ Object

Like #respond_to? but returns the result of the call if it does 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.



45
46
47
48
49
50
51
52
# File 'lib/garcon/core_ext/kernel.rb', line 45

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