My colleague Mark tells me that Ruby people love monkey patching, and has been pointing out situations where it can be used for fun and profit. One simple example is when another colleague, Eli, was having trouble with a fixture. Sadly fixtures don't have a way to know which function they are actually running, because they are passed an anonymous function by clojure.test. A prime candidate for monkey patching!
(use-fixtures :each
(fn [f]
(with-redefs [test-var identity]
(println "The test is:" (f)))))
To understand how this works you need to take a peek at the clojure.test code and notice that test-var is a function that executes the test. By redefing it we are able to just return the test instead.
If there is something you can't possibly do because the language or library implementer didn't provide a hook for it, you can probably do it with a redef. Fortunately the Clojure core code and most libraries are quite concise and easy to read, so finding a hook is usually pretty easy.
Obviously monkey patching is not something to do proudly or flagrantly, but it is a handy tool to have in the bag when you get stuck due to limitations in code outside of your control.
I'm super excited to be here, hope to talk with you tomorrow if you are attending!
To understand how this works you need to take a peek at the clojure.test code and notice that test-var is a function that executes the test. By redefing it we are able to just return the test instead.
If there is something you can't possibly do because the language or library implementer didn't provide a hook for it, you can probably do it with a redef. Fortunately the Clojure core code and most libraries are quite concise and easy to read, so finding a hook is usually pretty easy.
Obviously monkey patching is not something to do proudly or flagrantly, but it is a handy tool to have in the bag when you get stuck due to limitations in code outside of your control.
I'm super excited to be here, hope to talk with you tomorrow if you are attending!