-
-
Save hanachin/fe835513b7531176c446 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 PatternConverter | |
def initialize(pattern, &convert) | |
@pattern = pattern | |
@convert = convert | |
end | |
def ===(s) | |
@pattern === s | |
end | |
def convert(s) | |
@convert[s] | |
end | |
end | |
converters = [ | |
PatternConverter.new(/\d+/) {|s| s.to_i + 1 }, | |
PatternConverter.new(/[a-z]+/) {|s| s.upcase } | |
] | |
def get_converted_value(converters) | |
loop { gets.tap {|s| converters.find {|c| c === s }.tap {|c| return c.convert(s) if c } } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment