On Mar 17, 11:33 pm, Gary Wright <gwtm... / mac.com> wrote:
> On Mar 17, 2007, at 5:10 PM, hurcan solter wrote:
>
>
>
> > Although I am a seasoned C/C++ programmer(+10 years), I am relatively
> > new to the ruby (about 2 days) despite having some reservations(ruby
> > debugger should be theslowest thing in the world) i am otherwise very
> > impressed with the language, I intend to use Ruby for some code
> > generation task ,Everything was going well until i hit that roadblock
>
> > class REXML::Element
> >     include TSort
> >     def initialize
> >         @dependencies=[]
> >     end
> >     def tsort_each_node(&block)
> >         @elements.each(&block)
> >     end
> >     def tsort_each_child(node, &block)
> >         @dependencies.each(&block) if @dependencies.size>0
> >     end
> > end
>
> Just a guess but I suspect your problem is that you've effectively
> thrown away the initialize method for REXML::Element and replaced it
> with your own.  I think you want:
>
> alias __original_initialize initialize
> def initialize(*args, &block)
>    __original_initialize(*args, &block)
>    @dependencies = []
> end
>
> This way you make sure the original version of initialize
> executes as well as your new code.
>
> Gary Wright

On the contrary, it's the original version that executes not mine.
You see,it's the REXML module that creates the Element instance
and it calls the original constructor(if it's the term) not mine. I
erronously thought
i am effectively replacing the REXML:Element with my own version.
What i am trying to accomplish is adding to an existing class a new
instance variable
in its definition.

For example
require "rexml/document"
include REXML

class REXML::Element
     def some_method
         puts "some_method called"
     end

doc = Document.new File.open("mydoc.xml")
doc.each_element("somexmltag"){|node|    # i have no intervention here
REXML does all the stuff
    puts node.methods   #=>  ["some_method" "namespaces" "attributes"]
etc etc
    node.some_method   #=> some_method called
the above code snippet works, and adds a method to an existing class
in some other module
i just wonder is it possible to add some instance variable in same
manner?