Metaprogramming Programs as Data Metaprogramming Programs that use - - PowerPoint PPT Presentation

metaprogramming
SMART_READER_LITE
LIVE PREVIEW

Metaprogramming Programs as Data Metaprogramming Programs that use - - PowerPoint PPT Presentation

Metaprogramming Programs as Data Metaprogramming Programs that use other programs as data Examples: Compilers Templating and Generics Refactoring Tools Reflective Programming Programs that use themselves as data Examples:


slide-1
SLIDE 1

Metaprogramming

Programs as Data

slide-2
SLIDE 2

Metaprogramming

Programs that use other programs as data Examples:

  • Compilers

○ Templating and Generics

  • Refactoring Tools
slide-3
SLIDE 3

Reflective Programming

Programs that use themselves as data Examples:

  • Inspect variables, classes, and methods
  • Create new variables, classes, and

methods

slide-4
SLIDE 4

Ruby

is a "scripting language" Also:

  • Interpreted
  • Reflective
  • Object-oriented
slide-5
SLIDE 5

Ruby

Everything is an

  • bject, including

classes and methods Everything inherits from the class Object, including classes and methods

slide-6
SLIDE 6

Ruby

Symbols are like global enums Used to identify methods and variables Examples:

  • :foo
  • :'1'
  • :'@foo'
slide-7
SLIDE 7

Ruby

Class Variable: @@var Instance Variable: @var An instance's class variables are a class's instance variables

slide-8
SLIDE 8

Ruby

array.each do |obj| ... end (1..10).inject(0) {|m,n| m + n} def foo(arg, &block) ... end def greet @names.each {|n| yield n} end

slide-9
SLIDE 9

Ruby

No multiple inheritance; mixins instead Inherited class variables aren't copied into the new class

slide-10
SLIDE 10

Ruby

class A @@words = [] def <<(word) @@words << word end def print puts @@words.join(' ') end end class B < A; end class C < A; end (v0 = B.new) << 'hello' (v1 = C.new) << 'world'

slide-11
SLIDE 11

Object Methods

class send extend method methods responds_to? instance_exec instance_variables method_missing

slide-12
SLIDE 12

Object Methods

instance_variable_defined?(symbol) instance_variable_get(symbol) instance_variable_set(symbol, object) symbol looks like :'@name' "Sets the instance variable names by symbol to object, thereby frustrating the efforts

  • f the class’s author to attempt to provide

proper encapsulation." - Ruby Documentation

slide-13
SLIDE 13

Module Methods

module_eval class_eval class_variable_defined? included instance_method instance_methods method_defined?

slide-14
SLIDE 14

Classes

inherited callback How do you access class variables?

Klass.instance_variable_get Klass.instance_variable_set

Anonymous Classes klass = Class.new do method definitions end

slide-15
SLIDE 15

Use Cases: method_missing

debugging dynamic function definition error reporting proxy objects method families

def method_missing(meth, *args, &block) if meth.to_s =~ / ^find_by_(.+)$ / run_find_by_method($1, *args, &block) else super end end

slide-16
SLIDE 16

Use Cases: define_method

reduce code duplication form closures code instrumentation

log = Logger.new meth = obj.method(name)

  • bj.define_method(name) do |*args|

log.info("Called #{name}") meth.call(*args) end

slide-17
SLIDE 17

Case Study: RLTK::AST

slide-18
SLIDE 18

Case Study: RLTK::AST

slide-19
SLIDE 19

Case Study: RLTK::AST

slide-20
SLIDE 20

Case Study: RLTK::Parser

Motivation:

  • Subclass RLTK::

Parser to create new parsers

  • Define any number of

parsers

  • Instantiate any number
  • f parsers

Problem: Superclass class

variables are shared between subclasses

slide-21
SLIDE 21

Case Study: RLTK::Parser

slide-22
SLIDE 22

Questions?