Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
rentalcustard / gist:4329899
Created December 18, 2012 17:17
Example of using an anonymous comparator class
public List<Foo> sort(List<Foo> foos) {
return Collections.sort(foos, new Comparator<Foo>() {
@Override
public int compare(Foo a, Foo b) {
//comparison logic
}
});
}
@rentalcustard
rentalcustard / inject_vs_plus.rb
Created November 13, 2012 18:13
inject vs plus
require 'benchmark'
class PerfTest
def with_inject(array)
mapped_vals.inject(array, :<<)
end
def with_plus(array)
array + mapped_vals
end
@rentalcustard
rentalcustard / gist:4059342
Created November 12, 2012 13:16
update_attributes(nil)
>> User.last.update_attributes(nil)
=> true
@rentalcustard
rentalcustard / Polymorphism.java
Created November 5, 2012 15:07
Polymorphism => late binding
class Doer {
public void doStuff(Speaker speaker) {
speaker.speak();
}
}
interface Speaker {
void speak();
}
@rentalcustard
rentalcustard / 01_pre.java
Created October 31, 2012 10:15
My experience of static typing.
class MasterOfCeremonies { //I hate naming in potted examples
public void handle(Dog dog) {
dog.speak();
}
}
class Dog {
public void speak() {
//something
}
@rentalcustard
rentalcustard / privacy_example_failing.rb
Created October 30, 2012 08:43
Ruby private methods
class PrivacyExample
def some_public_method
self.some_private_method
end
def some_private_method
"o hai"
end
private :some_private_method
end
@rentalcustard
rentalcustard / csv_parsing.rb
Created July 31, 2012 14:27
Stateful CSV parsing sketch - pseudocode-ish.
class CSVParser
def initialize(line_handler=nil)
@line_handler = line_handler
@current_line = []
@lines = []
end
def receive(chunk)
#handle the chunk depending on state
#if we detect end of line:
@rentalcustard
rentalcustard / 1test.rb
Created June 2, 2012 09:05
Constant resolution in instance_eval
class A
B = Module.new
def ieval(&block)
instance_eval(&block)
end
end
B = Module.new
@rentalcustard
rentalcustard / file_dedupe.sh
Created November 29, 2011 09:25
file deduping
#!/bin/zsh
typeset -A files_by_sum
find $1 -maxdepth 1 | while read file; do
if [ -f $file ]; then #don't look at directories
sum=$(cat $file | md5sum | awk '{print $1}')
current=${files_by_sum[$sum]}
if [ $current ]; then
rm $file
ln -s $(basename $current) $file
fi
@rentalcustard
rentalcustard / example.rb
Created November 23, 2011 16:25
"model.show_on(view)" Not "view.show(model)"
#What follows is an extremely contrived example of how this might
# look. Please don't pay attention to the incredibly naive JSON
# and HTML creation code, that's not supposed to be the point.
class Account
def show_on(view)
view.show_balance(self.balance)
view.show_holder(self.holder_name)
view.show_creation_date(self.created_at)
end