Recipe 17.1. Running Code Only in Debug Mode
Problem
You want to print out
debugging messages or run some sanity-checking code, but only while you're developing your application;, not when you're running it in production.
Solution
...
Recipe 17.2. Raising an Exception
Credit: Steve Arneil
Problem
An error has occurred and your code can't keep running. You want to indicate the error and let some other piece of code handle it.
Solution
Raise an exception by ca...
Recipe 17.3. Handling an Exception
Credit: Steve Arneil
Problem
You want to handle or recover from a raised exception.
Solution
Rescue the exception with a begin/rescue block. The code you put into the rescue clause should h...
Recipe 17.4. Rerunning After an Exception
Credit: Steve Arneil
Problem
You want to rerun some code that raised an exception, having (hopefully) fixed the problem that caused it in the first place.
Solution
Retry the code that failed by e...
Recipe 17.5. Adding Logging to Your Application
Problem
You want to make your application log events or diagnostic data to a file or stream. You want verbose logging when your application is in development, and more taciturn logging when in pro...
Recipe 17.6. Creating and Understanding Tracebacks
Problem
You are debugging a program, and need to understand the stack traces that come with Ruby exceptions. Or you need to see which path the Ruby interpreter took to get to a certain lin...
Recipe 17.7. Writing Unit Tests
Credit: Steve Arneil
Problem
You want to write some unit tests for your software, to guarantee its correctness now and in the future.
Solution
Use
Test::Unit, the Ruby unit testing framework, from ...
Recipe 17.8. Running Unit Tests
Credit: Steve Arneil
Problem
You want to run some or all of the unit tests you've written.
Solution
This solution uses the example test class PersonTest from the previous recipe, Recipe 17.7. In that scena...
Recipe 17.9. Testing Code That Uses External Resources
Credit: John-Mason Shackelford
Problem
You want to test code without triggering its real-world side effects. For instance, you want to test a piece of code that makes an expensive net...
Recipe 17.10. Using breakpoint to Inspect and Change the State of Your Application
Problem
You're
debugging an application, and would like to be able to stop the program at any point and inspect the application's state (variables, data ...
Recipe 17.11. Documenting Your Application
Problem
You want to create a set of API
documentation for your application. You might want to go so far as to keep all your
documentation in the same files as your source code.
Solution
It'...
Recipe 17.12. Profiling Your Application
Problem
You want to find the slowest parts of your application, and speed them up.
Solution
Include the Ruby
profiler in your application with include 'profile' and the
profiler will start tr...
Recipe 17.13. Benchmarking Competing Solutions
Problem
You want to see which of two solutions to a problem is faster. You might want to compare two different algorithms, or two libraries that do the same thing.
Solution
Use the
be...
Recipe 17.14. Running Multiple Analysis Tools at Once
Problem
You want to combine two analysis tools, like the Ruby profiler and the Ruby tracer. But when one tool calls
set_trace_func, it overwrites the trace function left by the other...
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.