Skip to content

Instantly share code, notes, and snippets.

@cdeszaq
Last active December 19, 2015 01:59
Show Gist options
  • Select an option

  • Save cdeszaq/5880181 to your computer and use it in GitHub Desktop.

Select an option

Save cdeszaq/5880181 to your computer and use it in GitHub Desktop.

Revisions

  1. cdeszaq revised this gist Jul 1, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Solution.groovy
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    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()
  2. cdeszaq revised this gist Jul 1, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Problem.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    TODO: Fill in the problem statement
  3. cdeszaq renamed this gist Jul 1, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. cdeszaq revised this gist Jul 1, 2013. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions gistfile1.groovy
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    import java.text.NumberFormat

    //ExpandoMetaClass.enableGlobally()
    // 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) {
    // 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"
    // 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
    // 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())
    }
  5. cdeszaq created this gist Jun 27, 2013.
    79 changes: 79 additions & 0 deletions gistfile1.groovy
    Original 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())
    }