On 17.03.2007 22:33, Gary Wright 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.

While this is true, at least @dependencies should be properly 
initialized unless REXML::Element instances are not created via new.

There's another gotcha: by including TSort initialization might be 
broken, if TSort defines initialize and does not propagate to the parent 
class.

To OP: "if @dependencies.size>0" can be rewritten as "unless 
@dependencies.empty?" which again is superfluous here: #each will take 
care of empty collections.

Maybe you need provide more code.  From what you have shown so far it is 
not clear to me why @dependencies would be nil.

Kind regards

	robert