Skip to content

Instantly share code, notes, and snippets.

@hanachin
Forked from tompng/pattern.rb
Last active August 29, 2015 14:01
Show Gist options
  • Save hanachin/fe835513b7531176c446 to your computer and use it in GitHub Desktop.
Save hanachin/fe835513b7531176c446 to your computer and use it in GitHub Desktop.
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