O'Reilly logo

Metaprogramming Ruby 2 by Paolo Perrotta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

gems/activerecord-1.0.0/lib/active_record/base.rb
 
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 { |attr| ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required