RosettaCodeData/Task/Execute-a-Markov-algorithm/Ruby/execute-a-markov-algorithm.rb
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

20 lines
558 B
Ruby

raise "Please input an input code file, an input data file, and an output file." if ARGV.size < 3
rules = File.readlines(ARGV[0]).inject([]) do |rules, line|
if line =~ /^\s*#/
rules
elsif line =~ /^(.+)\s+->\s+(\.?)(.*)$/
rules << [$1, $3, $2 != ""]
else
raise "Syntax error: #{line}"
end
end
File.open(ARGV[2], "w") do |file|
file.write(File.read(ARGV[1]).tap { |input_data|
while (matched = rules.find { |match, replace, term|
input_data[match] and input_data.sub!(match, replace)
}) and !matched[2]
end
})
end