'How to output "error line number" in ruby exception handling?

I have the below sample code:

begin
  # some code
  # some code
  # some code
rescue => error
   puts error

I get the error "string not matched" Which doesn't tell you where the error occured. But when I remove the exception handling wrapper I get:

my_script.rb:20:in `[]=': string not matched (IndexError)
        from my_script.rb:20:in `block in my_func'
        from my_script.rb:18:in `each'
        from my_script.rb:18:in `my_func'
        from my_script.rb:66:in `<main>'

Which is more detailed. How can I print a more detailed error message?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source