Skip to content

Instantly share code, notes, and snippets.

@simonwo
Created October 7, 2013 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwo/6868309 to your computer and use it in GitHub Desktop.
Save simonwo/6868309 to your computer and use it in GitHub Desktop.
Partial classes in Ruby. Generates a new, specialised class by defining some of it's initializer arguments ahead of time.
class Class
def partial(*prep)
Class.new(self) do
define_method(:initialize) do |*args|
super(*(prep + args))
end
end
end
def << (prep)
partial(prep)
end
end
# e.g.
# Logfile = File << "/tmp/test.txt"
# Logfile.new do |f| { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment