Metaprograming in Ruby Brian Sam-Bodden What s this all about? - - PowerPoint PPT Presentation

metaprograming in ruby
SMART_READER_LITE
LIVE PREVIEW

Metaprograming in Ruby Brian Sam-Bodden What s this all about? - - PowerPoint PPT Presentation

Metaprograming in Ruby Brian Sam-Bodden What s this all about? Well learn... mainly about Rubys Meta-programming power about a language doesnt treat you like a moron what happens when dynamic meets object-oriented


slide-1
SLIDE 1

Metaprograming in Ruby

Brian Sam-Bodden

slide-2
SLIDE 2

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

slide-3
SLIDE 3

Ruby

slide-4
SLIDE 4

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!

slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7

Ruby

slide-8
SLIDE 8

Ruby

is...

slide-9
SLIDE 9

Ruby

is...

Object-Oriented

slide-10
SLIDE 10

Ruby

is...

Object-Oriented General Purpose

slide-11
SLIDE 11

Ruby

is...

Object-Oriented Interpreted General Purpose

slide-12
SLIDE 12

Ruby

is...

Object-Oriented Dynamic Interpreted General Purpose

slide-13
SLIDE 13

Ruby

is...

Object-Oriented Dynamic Interpreted General Purpose Garbage-collected

slide-14
SLIDE 14

Ruby

is...

Object-Oriented Reflective Dynamic Interpreted General Purpose Garbage-collected

slide-15
SLIDE 15

Ruby

is...

Object-Oriented Reflective Dynamic Interpreted General Purpose Multi-paradigm Garbage-collected

slide-16
SLIDE 16

Ruby

is...

Object-Oriented Reflective Dynamic Interpreted General Purpose Multi-paradigm Garbage-collected Elegant

slide-17
SLIDE 17

Without Ruby there would be no Rails Ruby’s meta-programing is the key feature that makes Rails magical

slide-18
SLIDE 18

Open Classes

slide-19
SLIDE 19
slide-20
SLIDE 20

Say we have an Array like:

slide-21
SLIDE 21

Say we have an Array like:

[1,2,3,4,5]

slide-22
SLIDE 22

Say we have an Array like:

[1,2,3,4,5]

and we want to sum of all of its elements

slide-23
SLIDE 23
slide-24
SLIDE 24

Let’s try something like:

slide-25
SLIDE 25

Let’s try something like:

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

slide-26
SLIDE 26

Let’s try something like:

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

slide-27
SLIDE 27

Let’s try something like:

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

Rats! Doesn’t work in Ruby

slide-28
SLIDE 28

Let’s try something like:

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

Rats! Doesn’t work in Ruby … yet!

slide-29
SLIDE 29
slide-30
SLIDE 30

Ruby classes are

slide-31
SLIDE 31

Ruby classes are

slide-32
SLIDE 32

Ruby classes are

We can teach the Array class a new behavior

slide-33
SLIDE 33

Ruby classes are

We can teach the Array class a new behavior

slide-34
SLIDE 34
slide-35
SLIDE 35

Let’s try again!

slide-36
SLIDE 36

Let’s try again!

slide-37
SLIDE 37

Let’s try again!

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

slide-38
SLIDE 38

Code as Data

Bend at will your code should

slide-39
SLIDE 39

When code can be manipulated as data a whole world of possibilities opens In Ruby you can evaluate the contents of a string

slide-40
SLIDE 40

Code that invokes code, that invokes code

slide-41
SLIDE 41

Code that invokes code, that invokes code

slide-42
SLIDE 42

Code that invokes code, that invokes code There you go, there you go

slide-43
SLIDE 43

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

slide-44
SLIDE 44

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

slide-45
SLIDE 45

Ruby provides the eval family of methods for runtime execution of code stored in strings eval will evaluate any string

slide-46
SLIDE 46

instance_eval can evaluate a string or a code block

slide-47
SLIDE 47

instance_eval can evaluate a string or a code block

slide-48
SLIDE 48

instance_eval can evaluate a string or a code block in the context of the receiver

slide-49
SLIDE 49

class_eval can evaluate a string or a code block in the context of the class or module it is called on

slide-50
SLIDE 50

class_eval can evaluate a string or a code block in the context of the class or module it is called on

slide-51
SLIDE 51

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

slide-52
SLIDE 52

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

slide-53
SLIDE 53

Meta- programming

slide-54
SLIDE 54

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

slide-55
SLIDE 55

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

  • pen and malleable

clean syntax code is data, data is code programming event model

slide-56
SLIDE 56

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

slide-57
SLIDE 57

Singleton Class

slide-58
SLIDE 58

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

slide-59
SLIDE 59

class_eval indirectly uses the meta-class adding a singleton method to the class

slide-60
SLIDE 60

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

slide-61
SLIDE 61

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

slide-62
SLIDE 62

Shortcuts such as define_method exist to make life easier

slide-63
SLIDE 63

Accessing the singleton meta-class explicitly

slide-64
SLIDE 64

Child classes get enhanced with class methods

slide-65
SLIDE 65

Inheritance the Ruby Way

slide-66
SLIDE 66

Many Ruby DSLs create class methods on subclasses using the meta-class (singleton class)

slide-67
SLIDE 67

Rails uses this technique in ActiveRecord

slide-68
SLIDE 68

A module with a “Base” class

slide-69
SLIDE 69

Methods get added to our classes with simple declarations Getting closer to a DSL!

slide-70
SLIDE 70

Now our class has a new set of class methods

slide-71
SLIDE 71

Now our class has a new set of class methods

slide-72
SLIDE 72

AOP the Ruby Way

slide-73
SLIDE 73

With alias_method you can wrap an existing method...

slide-74
SLIDE 74

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

slide-75
SLIDE 75

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

slide-76
SLIDE 76

Meta-Programming

Events

slide-77
SLIDE 77

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

Included Hook

slide-78
SLIDE 78

Method Missing

slide-79
SLIDE 79

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

method_missing method_added method_removed

slide-80
SLIDE 80

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

slide-81
SLIDE 81

Let’s implement the greeter example using method_missing

slide-82
SLIDE 82

greeter using method_missing

slide-83
SLIDE 83

greeter using method_missing

slide-84
SLIDE 84

Trellis

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

in between consulting gigs

and your friends no longer call you
slide-85
SLIDE 85

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

slide-86
SLIDE 86

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

slide-87
SLIDE 87

You might be asking yourself, but why?

slide-88
SLIDE 88

Why not?

slide-89
SLIDE 89

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

slide-90
SLIDE 90

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)

  • r stateful
slide-91
SLIDE 91

The simplest Trellis App, one Application and one Page

The Code The Template

slide-92
SLIDE 92

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)

slide-93
SLIDE 93

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;-)

slide-94
SLIDE 94

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

slide-95
SLIDE 95

What have we learned?

slide-96
SLIDE 96

There is more to Ruby than Rails! Building a Framework? Give Ruby a try! Build the language up towards the problem

slide-97
SLIDE 97

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

slide-98
SLIDE 98

www.integrallis.com