使用ERB,打印成HTML。 每当数组值可以被2整除时,我想将字体颜色更改为红色。 每当值包含3时,我想将字体颜色更改为蓝色。
如何编写input.txt?
这是example.rb的代码
require 'erb'
input_file = "input.txt"
output_file = "output.html"
prime_number = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
59, 61, 67, 71, 73, 79, 83, 89, 97]
str = ""
File.open(input_file, "r:UTF-8") do |io|
str = io.read
end
erb = ERB.new(str)
result = erb.result(binding)
File.open(output_file, "w") do |io|
io.write result
end
这是input.txt
<% prime_number.each |a| % 2 == 0 do %>
<% puts a %>
<% end %>