Ruby Warts

misfeatures and other problems
PERL| PHP| PYTHON| RUBY
Answers: (all questions)
(question)
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
Answer:
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.
PERL| PHP| PYTHON| RUBY
Last modified: March 30, 2006 @ 5:53 MST
Copyright (C) 1996-2017 Selene Scriven