Справка (вывести список всех команд). | |
quit | Выйти из отладчика. |
В листинге 16.4 приведен код простой программы (даже слишком простой, чтобы ее отлаживать).
STDOUT.sync = true
def palindrome?(word)
word == word.reverse
end
def signature(w)
w.split('').sort.join
end
def anagrams?(w1,w2)
signature(w1) == signature(w2)
end
print 'Give me a word: '
w1 = gets.chomp
print 'Give me another word: '
w2 = gets.chomp
verb = palindrome?(w1) ? 'is' : 'is not'
puts ''#{w1}' #{verb} a palindrome.'
verb = palindrome?(w2) ? 'is' : 'is not'
puts ''#{w2}' #{verb} a palindrome.'
verb = anagrams?(w1,w2) ? 'are' : 'are not'
puts ''{w1}' and '#{w2}' #{verb} anagrams.'
В листинге 16.5 показан полный сеанс отладки. Некоторые места не слишком понятны, потому что на консоль выводят данные и сама программа, и отладчик.
$ ruby -rdebug db.rb
Debug.rb
Emacs support available.
db.rb:1:STDOUT.sync = true
(rdb:1) b palindrome?
Set breakpoint 1 at db.rb:palindrome?
(rdb:1) b anagrams?
Set breakpoint 2 at db.rb:anagrams?
(rdb:1) b
Breakpoints:
1 db.rb:palindrome?
2 db.rb:anagrams?
(rdb:1) n
db.rb:3:def palindrome?(word)
(rdb:1) n
db.rb:7:def signature(w)
(rdb:1) n
db.rb:11:def anagrams?(w1,w2)
(rdb:1) n
db.rb:15:print 'Give me a word: '
(rdb:1) n
Give me a word: db.rb:16:w1 = gets.chomp
(rdb:1) live
db.rb:16:undefined local variable or method 'live' for main:Object
(rdb:1) n
live
db.rb:18:print 'Give me another word: '
(rdb:1) n
Give me another word: db.rb:19:w2 = gets.chomp
(rdb:1) n
evil
db.rb:21:verb = palindrome?(w1) ? 'is' : 'is not'
(rdb:1) с
Breakpoint 1, palindrome? at db.rb:palindrome?
db.rb:3:def palindrome?(word)
(rdb:1) n
db.rb: 4: word == word.reverse
(rdb:1) word
'live'
(rdb:1) n
db.rb: 22: puts ''#{w1}' #{verb} a palindrome.'
(rdb:1) verb 'is not'
(rdb:1) n
'live' is not a palindrome.
db.rb:24:verb = palindrome?(w2) ? 'is' : 'is not'
(rdb:1) n
db.rb:24:verb = palindrome?(w2) ? 'is' : 'is not'
(rdb:1) n
Breakpoint 1, palindrome? at db.rb:palindrome?
db.rb:3:def palindrome?(word)
(rdb:1) n
db.rb:4: word == word.reverse
(rdb:1) c
'evil' is not a palindrome.
Breakpoint 2, anagrams? at db.rb:anagrams?
db.rb:11:def anagrams?(w1,w2)
(rdb:1) n