Reputation: 2612
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!
Upvotes: 2
Views: 215
Reputation: 754
open
is a method on the module Kernel that is included in Object
the parent class of all Ruby classes. What happens is that open(str)
in
class Parser
def open str
Nokogiri::HTML(open(str))
end
is calling your defined open
method on Parser recursively. If you change your method to
def open str
Nokogiri::HTML(Kernel.open(str))
end
you'll call the open
method on Kernel as intended.
Upvotes: 2
Reputation: 16720
You are having a stack level too deep (SystemStackError)
error because your method is beeing called recursively.
def open str
Nokogiri::HTML(open(str)) # here you call this same method over and over again
end
This happens because the method you defined is more close in context so it's the one picked to be called.
You can just rename your method as you figured out or you can do something like this to explicit call it on one receiver that has the open method you want to use defined
def open str
uri = URI.parse(str)
Nokogiri::HTML(uri.open)
end
Upvotes: 2
Reputation: 19789
What seems to be happening is that
def open str
Nokogiri::HTML(open(str))
end
is in a recursive loop which just causes the SystemStackError
making the stack level too deep.
What exactly are you trying to do with open(str)? When you changed the open
to docopen
, where exactly did you change it?
Upvotes: 2