2007年1月9日星期二

Substituting variables into an Existing String

From last recipe,we know a way of substituting variables into string. Here, there are some ways again.



1. Printf-style string format
var = 'My name is %s.'
var % 'Allen'
#=> "My name is Allen."
'To 2 decimal places: %.2f' % Math:PI
#=> "To 2 decimal places: 3.14"
'Zero-padded: %.5d' % Math:PI
#=> "Zero-padded: 00003"




2.Use ERB
require 'erb'

template = ERB.new %q{It is <%= var%>!}
var = "here"
# When var is binded to template,just like following
puts template.result(binding)
#=> print "It is here"

1 条评论:

匿名 说...

只是想说出一个伟大的博客,你来到这里!我一直在相当多的时间也很多,但最终决定以显示我对你的工作表示赞赏!竖起大拇指,并不断进行下去!.