ruby metaprogramming

23
Ruby Metaprogramming and other languages…

Upload: mala

Post on 24-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

Ruby Metaprogramming. and other languages… . Metaprogramming. Writing programs that write programs (cool!) Often used to create domain-specific languages. Example*. class Module private def readonly (* syms ) # do nothing if no args return if syms.size == 0 - PowerPoint PPT Presentation

TRANSCRIPT

Ruby

Ruby Metaprogrammingand other languages 1MetaprogrammingWriting programs that write programs (cool!)Often used to create domain-specific languages (DSL)

Youve all heard of at least one DSL written in Ruby. Can you guess what it is? Example*class Moduleprivate

def readonly(*syms) # do nothing if no args return if syms.size == 0 # code starts as empty string code = ""

# generate Ruby code to define # attr reader methods # Notice how the symbol is # interpolated into the code syms.each do |s| code