Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… ·...

98
Metaprograming in Ruby Brian Sam-Bodden

Transcript of Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… ·...

Page 1: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Metaprogramingin RubyBrian Sam-Bodden

Page 2: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

What’s this all about?

! We’ll learn...

! mainly about Ruby’s Meta-programming power

! about a language doesn’t treat you like a moron

! what happens when dynamic meets object-oriented

! why Ruby is superb for building frameworks

! what happens when meta-programming

! ...meets someone with too much free-time

Page 3: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby

Page 4: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby Ideals:

Programming should be fun!

“I believe people want to express themselves when they program. They don’t want to fight with the language.

Programming languages must feel natural to programmers.”

Matz

The language should treat you like a responsible adult!

Page 5: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient
Page 6: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient
Page 7: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby

Page 8: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

Page 9: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

Page 10: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

"General Purpose

Page 11: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

" Interpreted

"General Purpose

Page 12: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

"Dynamic

" Interpreted

"General Purpose

Page 13: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

"Dynamic

" Interpreted

"General Purpose

" Garbage-collected

Page 14: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

"Reflective "Dynamic

" Interpreted

"General Purpose

" Garbage-collected

Page 15: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

"Reflective "Dynamic

" Interpreted

"General Purpose

" Multi-paradigm

" Garbage-collected

Page 16: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rubyis...

"Object-Oriented

"Reflective "Dynamic

" Interpreted

"General Purpose

" Multi-paradigm

" Garbage-collected

"Elegant

Page 17: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Without Ruby there would be no Rails

Ruby’s meta-programing is the key feature that makes Rails magical

Page 18: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Open Classes

Page 19: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient
Page 20: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Say we have an Array like:

Page 21: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Say we have an Array like:

[1,2,3,4,5]

Page 22: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Say we have an Array like:

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

all of its elements

Page 23: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient
Page 24: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try something like:

Page 25: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try something like:

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

Page 26: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try something like:

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

Page 27: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try something like:

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

Rats! Doesn’t work in Ruby

Page 28: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try something like:

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

Rats! Doesn’t work in Ruby

… yet!

Page 29: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient
Page 30: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby classes are

Page 31: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby classes are

Page 32: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby classes are

We can teach the Array class a new behavior

Page 33: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby classes are

We can teach the Array class a new behavior

Page 34: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient
Page 35: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try again!

Page 36: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try again!

Page 37: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s try again!

All you need to do is load the file containing the enhancements

Page 38: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Code as Data

Bend at will your code should

Page 39: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

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

In Ruby you can evaluate the contents of a string

Page 40: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Code that invokes code, that invokes code

Page 41: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Code that invokes code, that invokes code

Page 42: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Code that invokes code, that invokes code

There you go, there you go

Page 43: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby provides the eval family of methods for runtime execution of

code stored in strings

Page 44: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby provides the eval family of methods for runtime execution of

code stored in strings

Page 45: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby provides the eval family of methods for runtime execution of

code stored in strings

eval will evaluate any string

Page 46: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

instance_eval can evaluate a string or a code block

Page 47: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

instance_eval can evaluate a string or a code block

Page 48: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

instance_eval can evaluate a string or a code block

in the context of the receiver

Page 49: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

class_eval can evaluate a string or a code block

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

Page 50: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

class_eval can evaluate a string or a code block

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

Page 51: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

In Ruby we try to think about sending a message to an object rather than calling a method on an object

Page 52: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

In Ruby we try to think about sending a message to an object rather than calling a method on an object

In simple cases (above) syntactic sugar hides it, but we can use it in interesting ways...

Page 53: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Meta-programming

Page 54: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

...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 55: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby’s is a great vehicle for meta-programming because it is:

dynamic and reflexive

open and malleable

clean syntax

code is data, data is code

programming event model

Page 56: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

...uses meta-programming to bring the language closer to the problem

...is a collection of domain-specific languages (DSL) for building web

applications

Ruby on Rails

Page 57: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Singleton Class

Page 58: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

In Ruby you can enhance a particular instance of a class

Ruby uses a proxy class known as the singleton class

Meta-class: The singleton for Class objects

Page 59: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

class_eval indirectly uses the meta-class

adding a singleton method to the class

Page 60: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

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

Page 61: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

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

Page 62: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Shortcuts such as define_method exist to make life easier

Page 63: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Accessing the singleton meta-class explicitly

Page 64: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Child classes get enhanced with class methods

Page 65: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Inheritance the Ruby Way

Page 66: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Many Ruby DSLs create class methods on subclasses using the

meta-class (singleton class)

Page 67: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rails uses this technique in ActiveRecord

Page 68: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

A module with a “Base” class

Page 69: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Methods get added to our classes with simple declarations

Getting closer to a DSL!

Page 70: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Now our class has a new set of class methods

Page 71: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Now our class has a new set of class methods

Page 72: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

AOP the Ruby

Way

Page 73: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

With alias_method you can wrap an existing method...

Page 74: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

...effectively intercepting any calls and injecting any desired behavior

Page 75: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

...effectively intercepting any calls and injecting any desired behavior

Page 76: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Meta-Programming

Events

Page 77: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby has a rich event model associated with static and dynamic

changes of the code

Included Hook

Page 78: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Method Missing

Page 79: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby provides several hook methods that can be used to created

custom behaviors

method_missing

method_added

method_removed

Page 80: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Ruby’s Markup Builder is an example of what can be achieved

with method missing

Page 81: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s implement the greeter example using method_missing

Page 82: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

greeter using method_missing

Page 83: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

greeter using method_missing

Page 84: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Trellis...or what happens when you have too much time

in between consulting gigs and your friends no longer call you

Page 85: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

I was bored...

In the Ruby world I’ve worked with Rails and Merb

...thinking of web apps in terms of request & response

meanwhile... in the Java world I was working with Tapestry and Wicket

...thinking about Pages, Components and Events

Page 86: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Started to hack to see what it would be to build something like Tapestry/Wicket in Ruby

...actually it all started as an learning exercise

...but then I kept going

...somehow I ended building component-oriented web framework in Ruby

...part of a conversation among friends about the need for IoC and AOP frameworks for dynamic languages

Page 87: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

You might be asking yourself,

but why?

Page 88: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Why not?

Page 89: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

My goals:

"Magic like Rails; less code more action

" Small like Camping... a micro-framework

"Components like Tapestry/Wicket

"Clean HTML templates...

"...but also programatic HTML generation

"Non-managed components, no pooling

Page 90: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

The big picture:

Trellis Application

Pages Components

has

has

{ declares pages used

declares a home page

dispatches request to pages

} {defines a template

provides event handlers

passes events to the components

provides reusable logic

responds to events

can be stateless (tags) or stateful

Page 91: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

The simplest Trellis App, one Application and one Page

The Code

The Template

Page 92: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

What did I use to put this thing together...

!A boat load of meta-programming

!Rack ...HTTP the Ruby way

!Radius ...XML tags for the templates

!Builder ...for building HTML/XML

!Hpricot, REXML ...parsing searching HTML/XML

!Paginator ...for duh, pagination

!Parts of Rails ...so far ActiveSupport (Inflectors)

Page 93: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Let’s do some code spelunking on the Trellis codebase...

yes, there are some things that I’m not proud of but...

...this is pre-alpha, unreleased v0.000001;-)

Page 94: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Some of the example applications that I’ve put together in order of complexity

!hilo

!guest_book

!hangman

!yui

!stateful_counters

!flickr

!simple_blog

!crud_components

Page 95: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

What have we learned?

Page 96: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

There is more to Ruby than Rails!

Building a Framework? Give Ruby a try!

Build the language up towards the problem

Page 97: Metaprograming in Rubyassets.en.oreilly.com/1/event/12/Metaprogramming in Ruby Presenta… · Meta-programming...it’s a superb tool for building frameworks...it’s the key ingredient

Rack: http://rack.rubyforge.org

Radius: http://radius.rubyforge.org

Hpricot: http://code.whytheluckystiff.net/hpricot

REXML: http://www.germane-software.com/software/rexml

Builder: http://builder.rubyforge.org

Paginator: http://paginator.rubyforge.org

YUI (Yahoo! UI Library): http://open.yahoo.com/yui

Trellis (Not Released Yet)

Trellis @ RubyForge: http://rubyforge.org/projects/trellis

Trellis Website: http://www.trellisframework.org

Resources