Created
April 20, 2010 14:48
-
-
Save draegtun/372559 to your computer and use it in GitHub Desktop.
Io metaprogramming example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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