(question)
Question:
Answer:
Question:
#!/usr/bin/ruby
class Book
def open(path)
@file = open(path)
end
end
f = open("/dev/zero")
puts f
b = Book.new
b.open("/dev/zero")
puts b
This program opens a file, prints the file's object ID, then crashes with
a segmentation fault.
The reason it crashes is that Book.open calls itself recursively, until the stack overflows.
This is a double wart, because not only does the program recurse into oblivion, but it segfaults instead of raising an exception to handle the problem.
The reason it crashes is that Book.open calls itself recursively, until the stack overflows.
This is a double wart, because not only does the program recurse into oblivion, but it segfaults instead of raising an exception to handle the problem.