ruby metaprogramming 08

57
Rails A Peek under the covers Brian Sam-Bodden

Upload: brian-sam-bodden

Post on 16-May-2015

6.628 views

Category:

Technology


0 download

DESCRIPTION

An introductory presentation about Ruby metaprogramming presented at CodeMash '08

TRANSCRIPT

Page 1: Ruby Metaprogramming 08

RailsA Peek under the covers

Brian Sam-Bodden

Page 2: Ruby Metaprogramming 08

Rails

Page 3: Ruby Metaprogramming 08

A framework...

for building database-backed web applications

that at first wow us with speed

Page 4: Ruby Metaprogramming 08

Rails can be seen as a collection of...

mini-languages

for implementing MVC

on the web

Page 5: Ruby Metaprogramming 08

then came the headlines...

and we all wondered...

what am I doing wrong?

Page 6: Ruby Metaprogramming 08

too much

the answer

and we reacted

the way we knew better

...with frameworks

Page 7: Ruby Metaprogramming 08

Lessons of Rails

Page 8: Ruby Metaprogramming 08

Integrated

Familiar idioms on every tier

Seamless front to back stack

Avoiding manually defining or gluing the tiers

Page 9: Ruby Metaprogramming 08

CoC

“Flexibility is overrated, Constraints are liberating”

Convention Over Configuration

Page 10: Ruby Metaprogramming 08

DRYDo it once, do it right and do it in the right place

Page 11: Ruby Metaprogramming 08

Tight feedback loop

We want the world and we want it NOW!

Instant Change

Page 12: Ruby Metaprogramming 08

Getting advanced behavior with very little code

Without sacrificing readability

Terse Expressiveness

Page 13: Ruby Metaprogramming 08

Did the clones catch up?

Yes and Not quite, Sort of

✓CoC

✓ Instant Change๏Terse Expressiveness

✓DRY

✓Integrated

Page 14: Ruby Metaprogramming 08

What’s Missing?

Page 15: Ruby Metaprogramming 08
Page 16: Ruby Metaprogramming 08

Rubyis...

✓Object-Oriented

✓Reflective ✓Dynamic

✓ Interpreted

✓General Purpose

✓ Multi-paradigm

✓ Garbage-collected

✓Elegant

Page 17: Ruby Metaprogramming 08

Without Ruby there would be no Rails

Ruby’s Dynamism is the key ingredient to the Rails framework

Page 18: Ruby Metaprogramming 08

Open Classes

Page 19: Ruby Metaprogramming 08

Say we have an Array like:

[1,2,3,4,5]and we want to sum of all of its elements

Page 20: Ruby Metaprogramming 08

Let’s try something like:

[1,2,3,4,5].sum

Doh! Doesn’t work in Ruby … yet!

Page 21: Ruby Metaprogramming 08

Ruby classes are open

We can teach the Array class a new behavior

Page 22: Ruby Metaprogramming 08

Let’s try again!

Load the file containing the enhancements

Page 23: Ruby Metaprogramming 08

Since Ruby classes are open...

You can statically enhance ANY class

Page 24: Ruby Metaprogramming 08

Code as Data

Page 25: Ruby Metaprogramming 08

When code can be manipulated as dataa whole world of possibilities opens

In Ruby you can evaluate the contents of a string

Page 26: Ruby Metaprogramming 08

Code that spits out code

Page 27: Ruby Metaprogramming 08

Ruby provides the eval family of methods for runtime execution of code stored in strings

eval will evaluate any string

Page 28: Ruby Metaprogramming 08

instance_eval can evaluate a string or a code block

in the context of the receiver

Page 29: Ruby Metaprogramming 08

class_eval can evaluate a string or a code block

in the context of the class or module it is called on

Page 30: Ruby Metaprogramming 08

Meta-programming

Page 31: Ruby Metaprogramming 08

...is about programs that write programs

Meta-programming

...it’s a superb tool for building frameworks

...it’s the key ingredient for building domain-specific languages

Page 32: Ruby Metaprogramming 08

Ruby is a great language for meta-programming because

...is dynamic and reflexive

...open and malleable

...clean unencumbered syntax

...code is data, data is code

...programming event model

Page 33: Ruby Metaprogramming 08

...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

Rails

...is a domain-specific language for building web applications

Page 34: Ruby Metaprogramming 08

Singleton Class

Page 35: Ruby Metaprogramming 08

All objects are open to modification

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

Page 36: Ruby Metaprogramming 08

With access to a class object’s meta-class we can then use Ruby meta-programming techniques to

enhance the class

Page 37: Ruby Metaprogramming 08

Rails relies heavily on Ruby’s ability to dynamically enhance a class

Page 38: Ruby Metaprogramming 08

The previous example could have also been accomplished using the define_method

Page 39: Ruby Metaprogramming 08

Accessing the singleton meta-class explicitly

Page 40: Ruby Metaprogramming 08

Inheritance the Ruby Way

Page 41: Ruby Metaprogramming 08

Rails uses this technique in ActiveRecord

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

Page 42: Ruby Metaprogramming 08

Let’s create a module with a “Base” class

Page 43: Ruby Metaprogramming 08

Methods get added to our classes with simple declarationsCan you say DSL!

Page 44: Ruby Metaprogramming 08

Rails uses this technique in ActiveRecord

Now our class has a new set of class methods

Page 45: Ruby Metaprogramming 08

AOP the Ruby Way

Page 46: Ruby Metaprogramming 08

With alias_method you can wrap an existing method

Page 47: Ruby Metaprogramming 08
Page 48: Ruby Metaprogramming 08

Meta-Programming Events

Page 49: Ruby Metaprogramming 08

Included Hook

Page 50: Ruby Metaprogramming 08

Ruby has a rich event model associated with static and dynamic changes of the code

Page 51: Ruby Metaprogramming 08

Method Missing

Page 52: Ruby Metaprogramming 08

Ruby provides several hook methods that can be used to created custom behaviors

method_missing

method_added

method_removed

Page 53: Ruby Metaprogramming 08

Let’s implement the greeter example using method_missing

Page 54: Ruby Metaprogramming 08
Page 55: Ruby Metaprogramming 08

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

Page 57: Ruby Metaprogramming 08