# N\_children cannot be used to get number of toplevel nodes

**URL:** <https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875>\
**Category:** Ruby-Gnome 2\
**Created:** [March 25, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875 "2009-03-25T18:51:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [March 25, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875/1 "2009-03-25T18:51:11Z")

</div>

Hi

I’m trying to get the numbers of rows in my liststore, but it seems I  
lack a functionality.

In C, you can call :

gtk\_tree\_model\_iter\_n\_children(GtkTreeModel \*tree\_model, GtkTreeIter  
\*iter)

with a NULL pointer to GtkTreeIter, and get the number of toplevel  
nodes.

From the doc :  
Returns the number of children that iter has. As a special case, if iter  
is NULL, then the number of toplevel nodes is returned.

However, since we can only call n\_children from an instanced  
Gtk::TreeIter object, I can’t reproduce the C behavior.

Am I missing a method somewhere in the API, or should I do something  
ugly like :

module Gtk  
class ListStore  
def n\_rows  
count = 0  
each { count += 1 }  
count  
end  
end  
end

regards

Simon A.

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [March 25, 2009, 10:09pm UTC](https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875/2 "2009-03-25T22:09:37Z")

</div>

Simon A. wrote:

> Hi
> 
> I’m trying to get the numbers of rows in my liststore, but it seems I  
> lack a functionality.
> 
> In C, you can call :
> 
> gtk\_tree\_model\_iter\_n\_children(GtkTreeModel \*tree\_model, GtkTreeIter  
> \*iter)

You want Gtk::TreeIter#n\_children

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [March 26, 2009, 2:30pm UTC](https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875/3 "2009-03-26T14:30:51Z")

</div>

Igor P. wrote:

> Simon A. wrote:
> 
> > Hi
> > 
> > I’m trying to get the numbers of rows in my liststore, but it seems I  
> > lack a functionality.
> > 
> > In C, you can call :
> > 
> > gtk\_tree\_model\_iter\_n\_children(GtkTreeModel \*tree\_model, GtkTreeIter  
> > \*iter)
> 
> You want Gtk::TreeIter#n\_children

Either I’m missing something, or I didn’t made myself clear enough.

irb(main):005:0\> Gtk::TreeIter.singleton\_methods  
=\> [“gtype”]

So, I can’t use Gtk::TreeIter.n\_children(whatever arguments).

And, as I said : “we can only call n\_children from an instanced  
Gtk::TreeIter object”

The important word here is “instanced”. Because, I cannot instance a  
pseudo root iter above the ones in the liststore. It would be the same  
for the top level nodes in a treestore.

I should have pasted the doc of the C function, which clearly says :

Returns the number of children that iter has. As a special case, if iter  
is NULL, then the number of toplevel nodes is returned.

“if iter is NULL” is not possible in the ruby gnome2 bindings, afaik.

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [March 26, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875/4 "2009-03-26T16:11:40Z")

</div>

Mike C. wrote:

> Perhaps I’m not understanding correctly. Would
> 
> i = tree\_model.iter\_first  
> num\_children = i.n\_children
> 
> do what you want?
> 
> I realize it isn’t exactly what you would do in C code,  
> but that may be a good thing. Something like
> 
> TreeIter::n\_children\_from\_model(tree\_model)
> 
> doesn’t make much sense to me (though pretty  
> easy to implement, I guess, if there is a reason  
> to do so).
> 
> ```
> MikeC
> 
> ```

No, it would not work. It would return the first iter, which has no  
child.

require ‘gtk2’

model = Gtk::ListStore.new(Integer)  
10.times { model.append }  
first\_iter = model.iter\_first  
puts first\_iter.n\_children  
count = 0  
model.each { count += 1 }  
puts count

will output

0  
10

and first\_iter.parent.n\_children wil error, since the parent of  
first\_iter is nil.

I agree, it would be silly in the TreeIter class.

However, it could prove usefull in TreeModel, like :

Gtk::TreeModel#root\_n\_children =\> Integer  
or  
Gtk::TreeModel#n\_children(Gtk::TreeIter = nil) =\> Integer (juste like  
the C function)  
or  
Gtk::TreeModel#nb\_toplevel\_iters

And it’s very easy to implement for ListStore, as in my first post, but  
there is already a C function, I don’t see a good reason not to use it  
:).

---

<div class="post-metadata">

**Author:** ![mazwak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mazwak](https://www.ruby-forum.com/u/mazwak)\
**Post date:** [March 26, 2009, 3:12pm UTC](https://www.ruby-forum.com/t/n-children-cannot-be-used-to-get-number-of-toplevel-nodes/163875/5 "2009-03-26T15:12:59Z")

</div>

Perhaps I’m not understanding correctly. Would

i = tree\_model.iter\_first  
num\_children = i.n\_children

do what you want?

I realize it isn’t exactly what you would do in C code,  
but that may be a good thing. Something like

TreeIter::n\_children\_from\_model(tree\_model)

doesn’t make much sense to me (though pretty  
easy to implement, I guess, if there is a reason  
to do so).

```
          MikeC
```
