这是我的Ruby代码:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
books.sort! {
|firstBook, secondBook|
boolean_value = firstBook <=> secondBook
print "first book is = '#{firstBook}'"
print " , second book is = '#{secondBook}'"
puts " and there compare result is #{boolean_value}"
}
问题:
- 这段代码运行一次后就会出现错误
in 'sort!': comparison of String with String failed (ArgumentError)
- 当firstbook = "Charlie and the Chocolate Factory"时,secondBook应该是"War and Peace",但是代码选择了"Utopia"进行比较。为什么?