Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created July 9, 2014 16:35
Show Gist options
  • Save kwatch/b4fa7006b7e90a5653be to your computer and use it in GitHub Desktop.
Save kwatch/b4fa7006b7e90a5653be to your computer and use it in GitHub Desktop.
ヒアドキュメントとERBとを組み合わせた例。欠点は、テンプレート内でエラーがあっても、ファイル名と行番号がうまく表示されないこと。
# -*- coding: utf-8 -*-
require 'erb'
erb = ERB.new <<'END'
<html>
<body>
<ul>
<% (1..3).each do %>
<li><%= x %></li>
<% end %>
</ul>
</body>
</html>
END
html = erb.result()
print "Content-Type: text/html;charset=utf-8\r\n"
print "\r\n"
print html
#### 実行結果:eRubyテンプレート内でエラーがあっても、正確なファイル名と行番号がわからない
# (erb):5:in `block in <main>': undefined local variable or method `x' for main:Object (NameError)
# from (erb):4:in `each'
# from (erb):4:in `<main>'
# from /opt/lang/ruby/1.9.3-p385/lib/ruby/1.9.1/erb.rb:838:in `eval'
# from /opt/lang/ruby/1.9.3-p385/lib/ruby/1.9.1/erb.rb:838:in `result'
# from ex-heredoc.rb:16:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment