6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ERB を使う場合は filename と lineno を指定するとデバッグで便利

Last updated at Posted at 2024-08-31

ERB オブジェクトには filenamelineno という attribute がある。

これに設定した値はエラー時の Stack Trace で表示される。

$ ruby run_erb_with_error.rb
run_erb_with_error.rb:6:in `<main>': undefined local variable or method `unknown_variable' for main (NameError)
        from ~/.anyenv/envs/rbenv/versions/3.3.1/lib/ruby/3.3.0/erb.rb:429:in `eval'
        from ~/.anyenv/envs/rbenv/versions/3.3.1/lib/ruby/3.3.0/erb.rb:429:in `result'
        from ~/.anyenv/envs/rbenv/versions/3.3.1/lib/ruby/3.3.0/erb.rb:414:in `run'
        from run_erb_with_error.rb:12:in `<main>'

別ファイルの ERB を参照する場合はそのファイル名を、インラインで ERB を記述する場合は、 __FILE__, __LINE__ を活用して設定しておくと便利。

References

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?