instance method
respond_to?
Ruby on Rails 4.2.9
Since v4.0.13Signature
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 define 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
-
namereq -
include_privateopt = false
Source
# File activerecord/lib/active_record/attribute_methods.rb, line 235
def respond_to?(name, include_private = false)
return false unless super
name = name.to_s
# If the result is true then check for the select case.
# For queries selecting a subset of columns, return false for unselected columns.
# We check defined?(@attributes) not to issue warnings if called on objects that
# have been allocated but not yet initialized.
if defined?(@attributes) && self.class.column_names.include?(name)
return has_attribute?(name)
end
return true
end
Defined in activerecord/lib/active_record/attribute_methods.rb line 235
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::AttributeMethods