Trouble with module/class inheritance

J

James Byrne

I have a class "ForexCASource" that does this:

class ForexCASource
include Nokogiri::XML

def initializer(...
...
return xchg_source
end

# Takes an xml source, default set to FOREX_SITE
# Returns a nokogiri xml document object
def xchg_source(source=FOREX_SITE)
@forex_doc = Nokogiri::XML(open(source))
rescue Exception => e
puts e
Rails::logger.error("ForexCASource unable to read #{source} \n
#{e}")
raise e
end
...

When new is called on this class (fx_xml_doc = ForexCASource.new) then I
expect to receive @forex_doc, which has a class of
"Nokogiri::XML::Document", and an inherited public method '#xpath'.

However, while the returned object has class ForexCASource, which is to
be expected, I do not get the #xpath method attached to the returned
object, which is not what I expected.

When I examine the returned object then I see:

puts fx_xml_doc.class.ancestors
ForexCASource
Nokogiri::XML
Object

This is critically different than "Nokogiri::XML::Document". What
changes the ancestor of @forex_doc from Nokogiri::XML::Document to
Nokogiri::XML and removes the #xpath method? What is it about
inheritance/mixin am I not understanding?
 
J

James Byrne

James said:
This is critically different than "Nokogiri::XML::Document". What
changes the ancestor of @forex_doc from Nokogiri::XML::Document to
Nokogiri::XML and removes the #xpath method? What is it about
inheritance/mixin am I not understanding?

I implemented my own xpath method which calls
Nokogiri::XML::Document.xpath on the internal instance variable
@forex_doc. This has resolved my problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top