Created
March 25, 2012 15:19
-
-
Save betawaffle/2197223 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 FormattedString < String | |
PATTERN = /:(\w+)/ | |
DEFER = lambda { |match| match } | |
def initialize str = '', pattern = nil | |
super str | |
pattern ||= PATTERN | |
@compiler = DEFER | |
@orig = str | |
@gsub = gsub! pattern | |
end | |
def compiler &block | |
@compiler = block if block | |
@compiler | |
end | |
def compiler= block | |
@compiler = block || DEFER | |
end | |
def compile! | |
@gsub.each &compiler | |
end | |
def revert! | |
replace @orig | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment