# Can't subclass XML::Document?

**URL:** <https://www.ruby-forum.com/t/cant-subclass-xml-document/92444>\
**Category:** Ruby\
**Created:** [April 19, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/cant-subclass-xml-document/92444 "2007-04-19T15:55:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 19, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/cant-subclass-xml-document/92444/1 "2007-04-19T15:55:56Z")

</div>

I’m working with libxml and I can’t seem to define a subclass of  
XML::Document. For example,

require ‘libxml’

class SubDocument \< XML::Document  
def initialize(name)  
super  
@sub\_name = name  
end

def sub\_method  
puts “the method”  
end  
end

> s = SubDocument.new(‘test’)  
> =\> \<?xml version="test"?\>

> s.respond\_to? ‘sub\_method’  
> =\> false

> s.class  
> =\> XML::Document

As you can see from the above, it’s as if I’m aliasing XML::Document,  
rather than subclassing it. I have also tried including XML and then  
defining the subclass as well as opening the XML module and then  
defining the subclass – same results in both cases. I’ve never come  
across this before. Is there a way to fix this or should I just write  
my class to contain an XML::Document?

Thanks,  
Jeremy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 19, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/cant-subclass-xml-document/92444/2 "2007-04-19T17:55:17Z")

</div>

Hi,

In message “Re: can’t subclass XML::Document?”  
on Thu, 19 Apr 2007 22:55:08 +0900, “removed\_email\_address@domain.invalid”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I’m working with libxml and I can’t seem to define a subclass of  
|XML::Document.

Probably, some extensions define their own “new” class method instead  
of the “initialize” method.

```
          matz.

```
