A History of Complexity

Instead of looking at the current implementation of attribute methods, let me go all the way back to 2004—the year that Rails 1.0.0 was unleashed on an unsuspecting world.

Rails 1: Simple Beginnings

In the very first version of Rails, the implementation of attribute methods was just a few lines of code:

 module​ ActiveRecord
 class​ Base
 def​ initialize(attributes = ​nil​)
  @attributes = attributes_from_column_definition
 # ...
 end
 
 def​ attribute_names
  @attributes.keys.sort
 end
 
 alias_method​ ​:respond_to_without_attributes?​, ​:respond_to?
 
 def​ respond_to?(method)
  @@dynamic_methods ||= attribute_names +
  attribute_names.collect { ...

Get Metaprogramming Ruby 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.