2 May 2009

Fragments of cool code

From time to time I stumble over a piece of code, that just looks "cool". For example date literals in Ruby:
class Fixnum
def /(part)
[self, part]
end
end

class Array
def /(final_part)
Time.mktime(final_part, self[1], self[0])
end
end

13/11/2008 # => Thu Nov 13 00:00:00 +0100 2008
or a proper name for a throw:
catch (Exception up) {
// log the error
throw up;
}
The best name for a JUnit 4 test fixture (free to choose) I ever saw was in a presentation given by Peter Petrov:
@Before
public void doBeforeEachTest() {
A good name for a Ruby binding was
module Regenerated
Inside = binding
end
eval(script, Regenerated::Inside) where the evaluation is done within the scope of another module Regenerated which acts as a namespace.

No comments: