Skip to content

Instantly share code, notes, and snippets.

@oleander
Created February 8, 2016 01:21
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 oleander/7593235de6b6d6f93445 to your computer and use it in GitHub Desktop.
Save oleander/7593235de6b6d6f93445 to your computer and use it in GitHub Desktop.
pre-processor.rb
class PreProcessor < Struct.new(:raw)
attr_accessor :store
def adjust!(regexp, &block)
inner_raw = @raw
@raw.scan(regexp) do
$~.names.each do |name|
value = $~[name]
key = SecureRandom.hex
inner_raw = inner_raw.gsub(value, "<#{key}>")
@store << {
value: yield(value.strip),
key: "<#{key}>"
}
end
end
inner_raw
end
end
processor = PreProcessor.new("THINGS 1. IS THIS A QUESTION? STUFF")
output = processor.adjust!(/\.\s+(?<question>[^.?]+\?)/) do |value|
"**#{value}**"
end
output # => "THINGS <d58e3582afa99040e27b92b13c8f2280> STUFF"
processor.store # => [{ value: **1. IS THIS A QUESTION?**, key: "d58e3582afa99040e27b92b13c8f2280" }]
formated = output.downcase # => "things <d58e3582afa99040e27b92b13c8f2280> stuff"
processor.store.each do |data|
formated.sub!(data.key, data.value)
end
formated # => "things 1. IS THIS A QUESTION? stuff"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment