Forum: Ruby Trouble with module/class inheritance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-04-22 16:32
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?
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-04-22 16:41
James Byrne wrote:

>
> 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.
This topic is locked and can not be replied to.