Regular expressions (used with the tool
of your choice such as sed, Perl, or egrep) provide a more powerful query language
for finding patterns within large bodies of source.
??? Search the Web. The popularity of Rails has created a fury of bloggers who write
about Ruby and Rails, and they usually fill in the gaps where the official documentation
is lacking. Google Code Search* indexes thousands of open source
projects, and has useful features such as search by regular expression.
Reading the Call Stack
Most Rails developers are familiar with reading a stack trace when debugging exceptions.
It can be very helpful to know the sequence in which framework and application
functions were called when something goes wrong. But what if we are just
curious? It doesn??™t make sense to raise an exception just to generate a readable backtrace.
Luckily, we have some tools at our disposal to analyze the call stack in running
code, nondestructively.
The first such tool is Ruby??™s standard Kernel#caller method, which gives us a simple
backtrace as an array of strings:
#!/usr/local/bin/ruby1.8.4
# use the pretty-printer; call stacks can be huge
require 'pp'
class Test
def foo
bar
end
* http://www.google.com/codesearch
How to Read Code | 51
def bar
pp caller
'baz'
end
end
puts Test.new.foo
This gives us the call stack:
["./call_stack1.8.4.rb:8:in `foo'", "./call_stack1.
Pages:
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88