# REXML::Parent#index does not return nil

**URL:** <https://www.ruby-forum.com/t/rexml-parent-index-does-not-return-nil/101263>\
**Category:** Ruby\
**Created:** [February 5, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/rexml-parent-index-does-not-return-nil/101263 "2006-02-05T13:40:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TAKANO\_Noriyuki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@TAKANO\_Noriyuki](https://www.ruby-forum.com/u/TAKANO_Noriyuki)\
**Post date:** [February 5, 2006, 1:40pm UTC](https://www.ruby-forum.com/t/rexml-parent-index-does-not-return-nil/101263/1 "2006-02-05T13:40:14Z")

</div>

Hi all,

This is my first posting to ruby-talk.  
I regularly use REXML, appreciating those who are making and  
supporting it. But wishing it (or bug?) would be fixed, I  
wonder if this mailing list is an appropriate place to report  
a problem I see in REXML.

problem:  
REXML::Parent#index(obj) does not return nil but the last index  
of children of the parent  
when the argument is not a child of the parent.  
In the ruby-doc of REXML::Parent, parent.index method is described:  
index(child)  
Fetches the index of a given child  
@param child the child to get the index of  
@return the index of the child, or nil if the object is  
not a child of this parent.

The cause for not returning nil is obvious in the source code of  
Parent#index (REXML 3.1.3)  
def index( child )  
count = -1  
@children.find { |i| count += 1 ; i.hash == child.hash }  
count  
end

Should it be something like this?  
def index( child )  
count = -1

- @children.find { |i| count += 1 ; i.hash == child.hash }

- @children.find { |i| count += 1 ; i.hash == child.hash } ? count : nil  
end

Sample code:  
require ‘rexml/document’  
include REXML  
root = Element.new(“root”)  
p [ Element.new(“a”, root), Element.new(“b”, root),  
Element.new(“c”)].collect{|ele|  
root.index(ele)  
}

alt = Element.new(“alt”)  
def alt.index( child )  
count = -1  
@children.find { |i| count += 1 ; i.hash == child.hash } ? count : nil  
end

p [Element.new(“a”, alt), Element.new(“b”, alt),  
Element.new(“c”)].collect{|ele|  
alt.index(ele)  
}

Regards  
nori

happy, may\_not\_be = programming.partition{|it| it.is\_a? RubyProgramming}
