Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created April 20, 2010 14:48
Show Gist options
  • Save draegtun/372559 to your computer and use it in GitHub Desktop.
Save draegtun/372559 to your computer and use it in GitHub Desktop.
Io metaprogramming example
#!/usr/bin/env io
# see: http://transfixedbutnotdead.com/2010/01/14/anyone-for-perl-6-metaprogramming/
# http://gist.github.com/276591
Ninja := Object clone do (
name ::= nil
)
drew := Ninja clone setName ("Drew")
adam := Ninja clone setName ("Adam")
###############################################
// reopen
Ninja battleCry := method (
"#{name} says zing!!!" interpolate println
)
drew battleCry
adam battleCry
###############################################
// add throwStar
drew throwStar := method (
"throwing star" println
)
drew throwStar
###############################################
// call method dynamically
# a := "battleCry" & doString (a) also work
drew doString("battleCry")
###############################################
// add "colour" method closure/ing
colourName := "black"
Ninja colour := method (
"#{name}'s colour is #{colourName}" interpolate println
)
###############################################
// Define method dyna
swordSymbol := "********"
drew swing := method (soundEffect,
"#{name} : #{swordSymbol} #{soundEffect}" interpolate println
)
drew swing ("slash!!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment