Created
October 7, 2013 13:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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