1

Ἀρχιμήδης
2

π

3

δος µοι πα στω, και ταν γαν κινάσω

4

λ
5

the ultimate
6

λ

μετά
7

meta
8

metaprogramming?

9

metaOO

10

Code Generation
The Safety Scissors Of Metaprogramming

11

Ruby2Ruby
12

(lambda do puts “hello world” end).to_ruby

13

class Widget < ActiveRecord::Base end

14

Ruby2Ruby.translate(Widget)

15

class Widget < ActiveRecord::Base has_one :foo end

16

Ruby2Ruby.translate(Widget)

17

18

19

20

21

ove r k ill
22

monkeypatching

23

tangled object

graph

24

stop monkey-patching!

25

nothing can ever protect you

from bad programming

26

nothing can ever protect you

from bad programming

27

28

29

tests specs
30

tests
y u need

specs
31

32

tests rawk

33

debuggers suck

34

debuggers suck
35

monkey-patch monkey-patching itself
36

meta-monkey-patching
37

rubinius
38

nodebox

39

code == data

40

at this point, I just demoed code from my blog. below is a link to the post:
http://gilesbowkett.blogspot.com/2008/03/simple-metaprogramming-logger-with.html (code samples are included)

41

42

simple object graph

43

nodebox

44

tangled object

graph

45

node

box

46

same tool

47

different output

48

proof of concept
49

50

foo

51

foo bar

52

foo bar baz

53

foo
bar

bar

foo

foo

foo

foo
bar

baz

bar baz bar
bar
baz

foo bar baz
baz

foo

foo
bar

baz

54

debuggers suck

55

some people want debuggers

56

57

“Ruby has no tool support!”

58

1 day

59

shock the monkey

60

Powerful Tools

61

Powerful Tools
• Rubinius • Nodebox • code == data

62

Powerful Tools
• Rubinius • Nodebox • code == data

63

Powerful Tools
• Rubinius • Nodebox • code == data

64

• Rubinius • Nodebox • code == data

65

code == data

66

1998

67

generated HTML

68

automated cvs and rcs

69

70

EJB
(2003)
71

legacy app: 150 tables

72

ejb: 7 files per entity

73

7 * 150 = 1050 Java files

74

by hand: 3+ man-years

75

generators: 2 man-months

76

77

four generator types
• • • • •
code munger generator inline code expansion generator mixed-code generator partial-class generator tier generator

78

generating UIs

79

generating unit tests

80

embedding SQL with generators

81

creating database access generators

82

generating web services layers

83

generating models from a business logic DSL

84

sound familiar?

85

86

script/generate scaffold

87

script/generate resource

88

script/generate plugin

89

script/generate migration

90

script/generate

91

rails Command

92

acts_as_authenticated

93

restful_authentication

94

ActiveRecord SQL

95

ActionPack Helpers
•ActiveRecordHelper •FormTagHelper •AssetTagHelper •JavascriptHelper •AtomFeedHelper •NumberHelper •BenchmarkHelper •PrototypeHelper TextRecordTagHelper •CacheHelper • •CaptureHelper •SanitizeHelper •DateHelper •ScriptaculousHelper •DebugHelper •TagHelper •FormHelper •TextHelper •FormOptionsHelper •UrlHelper •RecordIdentificationHelper

96

code generation FTW
97

98

code generation FTW
99

code == data

100

(eq (code (data)))

101

102

functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions which return functions

103

code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data

104

105

lisp macros pwn “metaprogramming”

106

and you can do them in Perl!

107

108

shocked? perl!

109

110

shocked? perl!

111

the ultimate
112

λ

combinator
113

$

114

small teams

$

115

small teams

$

lisp macros

116

great programmers can write better programmers than they can hire

117

great programmers can write better programmers than they can hire

metaprogramming
118

great?
119

120

intergalactic voluntarily bald clone wizards of time and space who the womens are be lusting fors

121

122

123

124

incredible

power

125

simple clarity
126

simplicity magic
127

skilled programmers can write better programmers than they can hire

metaprogramming
128

129

code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data which runs as code which generates data

130

Unix.is_a? Lisp

131

# => true

132

133

δος µοι πα στω, και ταν γαν κινάσω

134

Ruby.is_a? πα στω

135

# => true

136

137

138

139

140

141

boba fezz
142

143

Caveats!
• read the book, the technique is easy • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

144

Caveats!
• read the book, the technique is easy • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

145

Caveats!
• read the book, the technique is easy • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

146

Caveats!
• read the book, the technique is easy • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

147

Caveats!
• read the book, the technique is easy • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

148

149

Essential Technique
• read the book, the technique is easy • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

150

Essential Technique
• puts • leveraging it is the clever part • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

151

Essential Technique
• puts • eval • you can use Ruby as well as Rubinius • if Unix is a Lisp, the filesystem is a compiler

152

Essential Technique
• puts • eval • ERB • if Unix is a Lisp, the filesystem is a compiler

153

Essential Technique
• puts • eval • ERB if Unix is a •(unix pipes) Lisp, the filesystem is a compiler

154

Essential Technique
• open mouth • make noise • profit • if Unix is a Lisp, the filesystem is a compiler

155

156

Sign up to vote on this title
UsefulNot useful