Is it possible to use the method open on my Parser class? It seems that the method is conflicting with IO::open?
class Parser
require 'nokogiri'
def parse
doc = open "someFile.html"
# Get to parsin' ...
end
def open str
Nokogiri::HTML(open(str))
end
end
parser = Parser.new
parser.parse
When I run the script I get this error:
$ ruby parser.rb
parser.rb:10: stack level too deep (SystemStackError)
I’ve tried a variety of things, but the only thing that seems to work is to rename Parser::open to something other than open, like docopen
I’m trying to understand how ruby works, so any further explanation beyond the answer is greatly appreciated!
openis a method on the module Kernel that is included inObjectthe parent class of all Ruby classes. What happens is thatopen(str)inis calling your defined
openmethod on Parser recursively. If you change your method toyou’ll call the
openmethod on Kernel as intended.