Ruby Metaprogramming

40
Ruby Metaprogrammi ng

description

Ruby Metaprogramming Slides which spoke at 2009-12-22 in Ruby Tuesday, Taiwan.

Transcript of Ruby Metaprogramming

Page 1: Ruby Metaprogramming

Ruby Metaprogramming

Page 2: Ruby Metaprogramming

About me Lu Wei Jen http://blog.weijen.net http://www.facebook.c

om/weijenlu Twitter: weijenlu

Page 3: Ruby Metaprogramming

Metaprogramming Metaprogramming is writing code that

manipulates language constructs at runtime.

Page 4: Ruby Metaprogramming

Agenda Object, Class and Module Singleton Methods and Metaclass Block Eval family

Page 5: Ruby Metaprogramming

Part 1. Object, Class and Module Object introspection Classes Module Singleton Method Method Lookup Dynamic Methods Hook methods

Page 6: Ruby Metaprogramming

Object Introspection

Page 7: Ruby Metaprogramming

ª›®œ•Œ QuickTime˛ ©M°ß°®∏—¿£¡Yæπ

®”¿Àµ¯¶ππœµe°C

Metaprogramming Ruby p.38

Objects of the same class share methods, but they don’t share instance variables.

Page 8: Ruby Metaprogramming

Classes Classes themselves are nothing but

objects. Classes are instances of a class called

Class.

Page 9: Ruby Metaprogramming

Method Lookup One step to the right, then up.

obj

@val

MyClass

my_method()

class()

Object

Page 10: Ruby Metaprogramming

Open Class You can always reopen existing classes,

and modify them.

Page 11: Ruby Metaprogramming

Module Modules are also objects. When you include a module:

Ruby creates an anonymous class wraps the module.

Ruby then insert anonymous class into the ancestors chain.

Page 12: Ruby Metaprogramming

Module

obj@val

Myclassmy_method

(M)m_method

Objectclass

Page 13: Ruby Metaprogramming

Singleton Method

Page 14: Ruby Metaprogramming

Class Method are Singleton Method

Page 15: Ruby Metaprogramming

Metaclass Where is singleton method? An object can have its own special, hidden

class named Metaclass. Also named eigenclass.

obj@val

Myclassmy_method

(M)m_method

Objectclass

Page 16: Ruby Metaprogramming

Metaclass

Page 17: Ruby Metaprogramming

Define Class Methods

Page 18: Ruby Metaprogramming

What about module?

Page 19: Ruby Metaprogramming

Method Lookup Review

obj@val

MyClassmy_method

(M)m_method

Objectclass

#objmy_singleton_method

#MyClassclass_method_1class_method_2

#(M)m_class_method

#Object

Page 20: Ruby Metaprogramming

Define Methods Dynamically

Page 21: Ruby Metaprogramming

Calling Method Dynamically

Page 22: Ruby Metaprogramming

method_missing() If method can’t be find, then self calling

method_missing(). It’s an instance method of Kernel that every

object inherits. It responded by raising a NoMethodError.

Page 23: Ruby Metaprogramming

Overriding method_missing()

Page 24: Ruby Metaprogramming
Page 25: Ruby Metaprogramming

Hook methods inherited() included()

Page 26: Ruby Metaprogramming

Part 2. Block Block Scope Callable Objects

Page 27: Ruby Metaprogramming

Block

Page 28: Ruby Metaprogramming

Scope

(But don’t do that)

Page 29: Ruby Metaprogramming

Scope Gates Class definitions Module definitions methods

Page 30: Ruby Metaprogramming

Proc Object

Page 31: Ruby Metaprogramming

The & Operator A block can be an argument to a method. Block argument:

must be the last in the list of arguments. Must prefixed by an & sign.

& operator convert the block to a Proc object.

& operator also can convert the Proc object to block.

Page 32: Ruby Metaprogramming

The & Operator

Page 33: Ruby Metaprogramming

Part 3. Eval* family eval instance_eval module_eval(class_eval)

Page 34: Ruby Metaprogramming

Kernel#eval() it takes a string of code. It executes the code in the string.

Page 35: Ruby Metaprogramming

The troble with eval() Your editor’s features mayn’t support. Not easy to read and modify. Not easy to debug. Code Injection

Page 36: Ruby Metaprogramming

Object#instance_eval

Page 37: Ruby Metaprogramming

Module#class_eval(module_eval)

Page 38: Ruby Metaprogramming

Classes themselves are nothing but objects.

Page 39: Ruby Metaprogramming

Thanks

Page 40: Ruby Metaprogramming

Q&A