Ruby: respond_to?

默认的respond_to?只检查public的方法:

class User
  def hi
    puts 'hi'
  end

  private

  def hello
    puts 'hello'
  end
end

user = User.new
user.respond_to?(:hi)    # => true
user.respond_to?(:hello) # => false

如果想要检查protectedprivate的方法需要这样改:

user.respond_to?(:hello, true)