# REXML inheritance confusion (simple q?)

**URL:** <https://www.ruby-forum.com/t/rexml-inheritance-confusion-simple-q/121490>\
**Category:** Ruby\
**Created:** [November 28, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/rexml-inheritance-confusion-simple-q/121490 "2007-11-28T13:19:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 28, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/rexml-inheritance-confusion-simple-q/121490/1 "2007-11-28T13:19:02Z")

</div>

So I created my own subclass of REXML::Document. Like this:

#begin example  
class MyDoc \< REXML::Document  
def initialize ()  
@tree = super("")  
puts @tree.class  
@name = tree.root.add\_element(“test”)  
end  
#end example

The above results in “undefined method `root’ for nil:NilClass  
(NoMethodError)”

Also trying ‘super.new("")’ doesnt work.

I can get it working by writing @tree =  
REXML::Document(""), but then my nice class will only be a  
container, not a subclass.

How can I actually get it working? Ie. get my very own subclass of  
REXML::Document with my very own initialize.

Yes, its a foggy day and inheritance seems to escape me…

Csmr

---

<div class="post-metadata">

**Author:** ![Casimir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@Casimir](https://www.ruby-forum.com/u/Casimir)\
**Post date:** [November 28, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/rexml-inheritance-confusion-simple-q/121490/2 "2007-11-28T19:22:28Z")

</div>

Casimir wrote:

> So I created my own subclass of REXML::Document. Like this:
> 
> #begin example  
> class MyDoc \< REXML::Document  
> def initialize ()  
> @tree = super("")  
> puts @tree.class  
> @name = tree.root.add\_element(“test”)  
> end  
> #end example
> 
> The above results in “undefined method `root’ for nil:NilClass  
> (NoMethodError)”
> 
> Also trying ‘super.new("")’ doesnt work.
> 
> I can get it working by writing @tree =  
> REXML::Document(""), but then my nice class will only be a  
> container, not a subclass.
> 
> How can I actually get it working? Ie. get my very own subclass of  
> REXML::Document with my very own initialize.
> 
> Yes, its a foggy day and inheritance seems to escape me…
> 
> Csmr  
> try this:

-----BEGIN PROGRAM-----  
require ‘rexml/document’

xml =\<\<END\_DOC  
  
END\_DOC

class MyDoc \< REXML::Document  
def initialize (fnm)  
super(fnm)  
puts “from initialize: #{self.class}”  
@name = self.root.add\_element(“test”)  
end  
end

m\_doc = MyDoc.new(xml)  
puts m\_doc.root  
-----END PROGRAM-----
