![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1 template = 'hello %s'
2 puts template % 'ray'
3
4 puts 'To 2 decimal places: %.2f' % Math::PI
5
6 require 'erb'
7 template = ERB.new %q{hello <%= name %>!}
8 name = 'ray'
9 puts template.result(binding)
10 puts template.result
11
12 template = %q{
13 <% if problems.empty? %>
14 Looks like your code is clean!
15 <% else %>
16 I found the following possible problems with your code:
17 <% problems.each do |problem, line| %>
18 * <%= problem %> on line <%= line %>
19 <% end %>
20 <% end %>}.gsub(/^\s+/, '')
21
22 template = ERB.new(template, nil, '<>')
23 problems = [["Use of is_a? instead of duck typing", 23],
24 ["eval() is usually dangerous", 44]]
25 template.run(binding)
26
27 problems = []
28 template.run(binding)
29
30 class String
31 def substitute(binding=TOPLEVEL_BINDING)
32 eval(%{"#{self}"}, binding)
33 end
34 end
35
36 template = %q{hello #{name}!}
37
38 name = 'sunshine'
39 puts template.substitute(binding)