[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

REXML inheritance confusion (simple q?

Casimir

11/28/2007 12:11:00 PM

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

#begin example
class MyDoc < REXML::Document
def initialize ()
@tree = super("<docc></docc>")
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("<docc></docc>")' doesnt work.

I can get it working by writing @tree =
REXML::Document("<docc></docc>"), 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
1 Answer

Jose francisco Gonzalez carmona

11/28/2007 6:23:00 PM

0

Casimir wrote:
> So I created my own subclass of REXML::Document. Like this:
>
> #begin example
> class MyDoc < REXML::Document
> def initialize ()
> @tree = super("<docc></docc>")
> 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("<docc></docc>")' doesnt work.
>
> I can get it working by writing @tree =
> REXML::Document("<docc></docc>"), 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
<docc></docc>
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-----

--
Posted via http://www.ruby-....