Published on February 4, 2008
Rails A Peek under the covers Brian Sam-Bodden
A framework... for building database-backed web applications that at ﬁrst wow us with speed
Rails can be seen as a collection of... mini-languages for implementing MVC on the web
then came the headlines... and we all wondered... what am I doing wrong?
the answer too much and we reacted the way we knew better ...with frameworks
Lessons of Rails
Integrated Seamless front to back stack Familiar idioms on every tier Avoiding manually deﬁning or gluing the tiers
CoC Convention Over Conﬁguration “Flexibility is overrated, Constraints are liberating”
DRY Do it once, do it right and do it in the right place
Instant Change Tight feedback loop We want the world and we want it NOW!
Terse Expressiveness Getting advanced behavior with very little code Without sacriﬁcing readability
Did the clones catch up? Yes , Sort of and Not quite ✓CoC ✓DRY ๏ Terse Expressiveness ✓ Instant Change ✓Integrated
✓Object-Oriented ✓Elegant ✓General Purpose ✓ Multi-paradigm Ruby ✓ Interpreted is... ✓Reﬂective ✓Dynamic ✓ Garbage-collected
Without Ruby there would be no Rails Ruby’s Dynamism is the key ingredient to the Rails framework
Say we have an Array like: [1,2,3,4,5] and we want to sum of all of its elements
Let’s try something like: [1,2,3,4,5].sum Doh! Doesn’t work in Ruby … yet!
Ruby classes are open We can teach the Array class a new behavior
Let’s try again! Load the ﬁle containing the enhancements
Since Ruby classes are open... You can statically enhance ANY class
Code as Data
When code can be manipulated as data a whole world of possibilities opens In Ruby you can evaluate the contents of a string
Code that spits out code
Ruby provides the eval family of methods for runtime execution of code stored in strings eval will evaluate any string
instance_eval can evaluate a string or a code block in the context of the receiver
class_eval can evaluate a string or a code block in the context of the class or module it is called on
Meta-programming ...is about programs that write programs ...it’s a superb tool for building frameworks ...it’s the key ingredient for building domain-speciﬁc languages
Ruby is a great language for meta-programming because ...is dynamic and reﬂexive ...open and malleable ...code is data, data is code ...clean unencumbered syntax ...programming event model
Rails ...uses the Ruby meta-programming techniques to accomplish most of its “magic” ...uses meta-programming to bring the language closer to the problem at hand ...is a domain-speciﬁc language for building web applications
All objects are open to modiﬁcation You can change a particular instance of a class Ruby uses a proxy class known as the singleton class Meta-class: The singleton for Class objects
With access to a class object’s meta-class we can then use Ruby meta-programming techniques to enhance the class
Rails relies heavily on Ruby’s ability to dynamically enhance a class
The previous example could have also been accomplished using the deﬁne_method
Accessing the singleton meta-class explicitly
Inheritance the Ruby Way
Ruby Meta-programming techniques can be used to write DSLs that use class methods to enhance subclasses Using the meta-class (singleton class) of the subclasses Rails uses this technique in ActiveRecord
Let’s create a module with a “Base” class
Methods get added to our classes with simple declarations Can you say DSL!
Rails uses this technique in ActiveRecord Now our class has a new set of class methods
AOP the Ruby Way
With alias_method you can wrap an existing method
Ruby has a rich event model associated with static and dynamic changes of the code
Ruby provides several hook methods that can be used to created custom behaviors method_missing method_added method_removed
Let’s implement the greeter example using method_missing
There is more to Ruby than Rails! Building a Framework? Give Ruby a try! Build the language up towards the problem Meta-programming is no longer just for the Lisp folks The future? Powerful Java APIs exposed with Ruby DSLs
An introductory presentation about Ruby metaprogramming presented at CodeMash '08
on 2007-08-04 00:19 ... > Ari Brown wrote: >> Are there any suggested resources on learning the art of Ruby >> metaprogramming? > > Ruby for Rails, ...
Write powerful Ruby code that is easy to maintain and change. With metaprogramming, you can produce elegant, clean, and beautiful programs. Once the domain ...
- Metaprogramming Ruby 2: Program Like the Ruby Pros (Facets of Ruby) by jetzt kaufen. Kundrezensionen und 0.0 Sterne. …
Python Vs. Ruby for Metaprogramming ... – Pablo Oct 27 '08 at 15:05. ... The Pragmatic Programmers Ruby Metaprogramming series ...
What is Metaprogramming? Metaprogramming is code that writes code for you. But isn’t that what code generators do, like the rails gem, or yeoman? Or even ...
In this video, we'll learn about basic metaprogramming in Ruby. Metaprogramming refers to writing programs that can write themselves. We'll start by ...
Ruby Metaprogramming - Method Missing What is method_missing? method_missing is a method that ruby gives you access inside of your objects a way to handle ...