Frage

I got the following template:

|Info1|Info2|Info3|Info4|Info5|Info6|Info7|Info8|Info9|Info10|
|Info11|Info12|Info13|Info14|Info15|Info16|Info17|Info18|Info19|Info20|

And I have to substitute each information with real data and put it into a .txt file, what could be a good way to handle this in Ruby?

Thx

War es hilfreich?

Lösung

Place this in a file called subtemplate.rb:

subs = { 'Info1' => "Hello", 'Info2' => 'World' }

STDIN.each do |line|
    puts line.gsub(/\w+/) { |m| subs[m] }
end

Run from the command line:

 ruby subtemplate.rb < template.txt > output.txt

where template.txt contains the template file. Then output.txt is

|Hello|World|||||||||
|||||||||||
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top