instance method respond_to?

Ruby on Rails 8.1.2

Since v4.0.13

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

respond_to?(name, include_private = false)

A Person object with a name attribute can ask person.respond_to?(:name), person.respond_to?(:name=), and person.respond_to?(:name?) which will all return true. It also defines the attribute methods if they have not been generated.

class Person < ActiveRecord::Base
end

person = Person.new
person.respond_to?(:name)    # => true
person.respond_to?(:name=)   # => true
person.respond_to?(:name?)   # => true
person.respond_to?('age')    # => true
person.respond_to?('age=')   # => true
person.respond_to?('age?')   # => true
person.respond_to?(:nothing) # => false

Parameters

name req
include_private opt = false
Source
# File activerecord/lib/active_record/attribute_methods.rb, line 291
    def respond_to?(name, include_private = false)
      return false unless super

      # If the result is true then check for the select case.
      # For queries selecting a subset of columns, return false for unselected columns.
      if @attributes
        if name = self.class.symbol_column_to_string(name.to_sym)
          return _has_attribute?(name)
        end
      end

      true
    end

Defined in activerecord/lib/active_record/attribute_methods.rb line 291 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::AttributeMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close