Last active
December 19, 2015 01:59
-
-
Save cdeszaq/5880181 to your computer and use it in GitHub Desktop.
Revisions
-
cdeszaq revised this gist
Jul 1, 2013 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ // This can be used in lieu of changing the metaclass in the constructor, // but affects _all_ classes, which can be a big overhead // ExpandoMetaClass.enableGlobally() -
cdeszaq revised this gist
Jul 1, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ TODO: Fill in the problem statement -
cdeszaq renamed this gist
Jul 1, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cdeszaq revised this gist
Jul 1, 2013 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ import java.text.NumberFormat // This can be used in lieu of changing the metaclass in the constructor, // but affects _all_ classes, which can be a big overhead // ExpandoMetaClass.enableGlobally() class RobotDog { String name @@ -40,15 +43,14 @@ class RobotDog { } def methodMissing(String commandName, args) { // Create the method we'll call on subsequent commands def cachedMethod = { Object[] cmArgs -> // This is the default behavior when we don't know how to do something println "$name stares at you blankly" } // Add it to the class so that it'll be called next time instead of doing all this RobotDog.metaClass."$commandName" = cachedMethod // Adds it to the Class metaClass."$commandName" = cachedMethod // Adds it to the object @@ -57,6 +59,7 @@ class RobotDog { } } // Some instructions def input = '''jump bark paw @@ -72,8 +75,14 @@ fetch shoe chew leg hard ''' // Look, a new doggy! def rob = new RobotDog(name: "Rob the Dog") // Let's see what he can do... input.eachLine {line -> // Break the line into a list of pieces def instruction = line.tokenize() // Execute the line as a cons pair: Command at the head, followed by parameters rob."${instruction.head()}"(*instruction.tail()) } -
cdeszaq created this gist
Jun 27, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,79 @@ import java.text.NumberFormat //ExpandoMetaClass.enableGlobally() class RobotDog { String name RobotDog() { // Make the metaclass an expando so that we can cache behavior better def mc = new ExpandoMetaClass(RobotDog,false,true) mc.initialize() this.metaClass = mc } def jump() { println "$name jumps" } def jump(howHigh) { println "$name jumps $howHigh feet into the air" } def jump(howHigh, times) { println "$name jumps $howHigh feet into the air $times times" } def fetch(somethingToFetch) { println "$name fetches the $somethingToFetch" } def chew(somethingToChewOn) { println "$name chews on the $somethingToChewOn" } def chew(somethingToChewOn, howToChew) { println "$name chews on the $somethingToChewOn in a $howToChew way" } def bark() { println "$name barks" } def methodMissing(String commandName, args) { // println "method missing called" // Create the method we'll call on subsequent commands def cachedMethod = { Object[] cmArgs -> // println "${this.name} says: I don't know how to $commandName like this: $args" println "$name stares at you blankly" } // Add it to the class so that it'll be called next time RobotDog.metaClass."$commandName" = cachedMethod // Adds it to the Class metaClass."$commandName" = cachedMethod // Adds it to the object // Call it, since that's what we were asked to do return cachedMethod(args) } } def input = '''jump bark paw paw rollOver rollOver rollOver rollOver giveKisses jump 5 2 fetch ball fetch shoe chew leg hard ''' def rob = new RobotDog(name: "Rob the Dog") input.eachLine {line -> def instruction = line.tokenize() rob."${instruction.head()}"(*instruction.tail()) }