|
|||
Previous < |
Contents ^
|
Next >
|
-r debug
option, along with any other Ruby options and the name of
your script:
ruby -r debug [ options ] [ programfile ] [ arguments ] |
readline
support enabled, you can use cursor
keys to
move back and forth in command history and use line editing commands to
amend previous input.
To give you an idea of what the Ruby debugger is like, here
is a sample session.
% |
irb [ irb-options ] [ ruby_script ] [ options ] |
% irb irb(main):001:0> |
Figure not available... |
Meta-|
to execute Ruby. The Ruby interpreter
will use the selected region as standard input and output will go to a
buffer named ``*Shell Command Output*
.'' This feature has come in
quite handy for us while writing this book---just select a few lines
of Ruby in the middle of a paragraph and try it out!
You can do something similar in the vi editor using ``:!ruby
''
which replaces the program text with its output, or
``:w!ruby
'', which displays the output without
affecting the buffer. Other editors have similar features.
While we are on the subject, this would probably be a good place to
mention that there is a Ruby mode for Emacs included in the
distribution as misc/ruby-mode.el
. There are also several
syntax-highlighting modules for vim (an enhanced version of the vi
editor), jed, and other editors available on the net as well. Check
the Ruby FAQ for current locations and availability.
setter=
as an assignment to a local variable, not as a
method call. Use self.setter=
to indicate the method call.
end
keyword.
Object#type
to check the type
of an object.
{}
instead of do
/end
.
$stdout
and $stderr
, the output may
not appear in the order you were expecting. Always use nonbuffered
I/O (set sync=true
) for debug messages.
String
, and will not be
automatically converted to a number by Ruby. A call to to_i
will work wonders. A
common mistake Perl programmers make is:
while gets num1, num2 = split /,/ # ... end |
Hash#rehash
if it does).
trace_var
to watch when a variable changes value.
Object#freeze
. If you suspect that some unknown
portion of code is setting a variable to a bogus value, try
freezing the variable. The culprit will then be caught during the
attempt to modify the variable.
x
and y
variables
on each
iteration, but in the second version it doesn't. We'll use the
benchmark
package from the Ruby Application Archive to compare
the loops:
require "benchmark" include Benchmark n = 1000000 bm(12) do |test| test.report("normal:") do n.times do |x| y = x + 1 end end test.report("predefine:") do x = y = 0 n.times do |x| y = x + 1 end end end |
user system total real normal: 2.450000 0.020000 2.470000 ( 2.468109) predefine: 2.140000 0.020000 2.160000 ( 2.155307) |
-r
profile
, or from within the code using require
"profile"
. For example:
require "profile" class Peter def initialize(amt) @value = amt end def rob(amt) @value -= amt amt end end class Paul def initialize @value = 0 end def pay(amt) @value += amt amt end end peter = Peter.new(1000) paul = Paul.new 1000.times do paul.pay(peter.rob(10)) end |
time seconds seconds calls ms/call ms/call name 32.14 0.27 0.27 1 270.00 840.00 Fixnum#times 30.95 0.53 0.26 1000 0.26 0.27 Paul#pay 29.76 0.78 0.25 1000 0.25 0.30 Peter#rob 5.95 0.83 0.05 1000 0.05 0.05 Fixnum#- 1.19 0.84 0.01 1000 0.01 0.01 Fixnum#+ 0.00 0.84 0.00 1 0.00 0.00 Paul#initialize 0.00 0.84 0.00 2 0.00 0.00 Class#inherited 0.00 0.84 0.00 4 0.00 0.00 Module#method_added 0.00 0.84 0.00 1 0.00 0.00 Peter#initialize 0.00 0.84 0.00 1 0.00 840.00 #toplevel 0.00 0.84 0.00 2 0.00 0.00 Class#new |
Debugger commands
|
Previous < |
Contents ^
|
Next >
|