formatter = "%{first} %{second} %{third} %{fourth}"
puts formatter % {first: 1, second: 2, third: 3, fourth: 4}
puts formatter % {first: "one", second: "two", third: "three", fourth: "four"}
puts formatter % {first: true, second: false, third: true, fourth: false}
puts formatter % {first: formatter, second: formatter, third: formatter, fourth: formatter}
puts formatter % {
first: "I had this thing.",
second: "That you could type up right.",
third: "But it didn't sing.",
fourth: "So I said goodnight."
}
我很担心你会在ruby中使用格式字符串,有人可以解释一下。感谢
答案 0 :(得分:1)
在伪代码中:
MAIL_TEMPLATE = <<-TMPL
Hello, %{name}!
The support for %{product} will be ended at %{date} unless
due to insufficient amount of money on your account %{account}.
The amount to pay to continue using %{product}: %{debt}
TMPL
Storage.all_users.map(&:to_hash).each |user|
send_email(user.delete[:email], MAIL_TEMPLATE % user) if user[:debt]
end
以上内容可用于向帐户中没有足够金额的用户发送电子邮件。请注意,除了电子邮件模板外,还有2LOC。
请参阅以下@ mu-is-too-short的非常有价值的评论。