Stack Traces

When a script fails with an exception message, that message provides a description of the problem, in terms that may or may not make immediate sense to you. An exception message appears along with something called a stack trace, which is a kind of history of method calls going back to the top-level script.

The script in Listing C.1 has a problem (besides being obfuscated, contrived, and otherwise useless, that is) that prevents it from running to completion.

Listing C.1. div0.rb
#!/usr/bin/env ruby

def foo
  return 4.0/baz(20)
end

def baz(n)
  sum = 0
  n.times do |i|
    sum += quux(i)
  end
  return n + sum
end

def quux(v)
  return 9/(5-v)
end

puts foo

The Ruby interpreter produces the following information when trying to run the script:

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.