# Bug in ListStore?

**URL:** <https://www.ruby-forum.com/t/bug-in-liststore/226862>\
**Category:** Ruby-Gnome 2\
**Created:** [February 23, 2013, 8:55pm UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862 "2013-02-23T20:55:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [February 23, 2013, 8:55pm UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862/1 "2013-02-23T20:55:28Z")

</div>

Hi there,

I encountered some unexpected behaviour with the Gtk::ListStore class.  
Consider the following stand-alone Ruby script:

=========================================  
require “gtk2”

model = Gtk::ListStore.new(String, Symbol, Integer)  
row = model.append  
row[0] = “String”  
row[1] = :symbol  
row[2] = 10

# row2 = model.iter\_first p [row[0], row[1], row[2]]

I expected the array to come out as [“String”, :symbol, 10], however it  
seems that ListStore internally refuses to store symbols (although I  
explicitely requested a symbol to be stored). The actual output is:

[“String”, “symbol”, 10]

That is, the symbol has been converted to a string. I’m not sure  
whether this is a bug or expected behaviour, but if the latter, I was  
unable to find it documented somewhere. Can someone light up this?

OS: Arch Linux 64 bits  
Ruby version: ruby 1.9.3p385 (2013-02-06 revision 39114) [x86\_64-linux]  
ruby-gtk2 version: 1.2.1

Valete,  
Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [February 24, 2013, 4:11am UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862/2 "2013-02-24T04:11:07Z")

</div>

Hello Quintus,

Looks like there is a bug in the way that Gtk::TreeModel/TreeStore saves  
Symbol information, it automatically converts a Symbol to a String  
class,  
since Symbols are un-mutable Strings. The easiest method in which to do  
this is to subclass TreeModel/TreeStore and save the column information  
in  
ruby, and properly handle symbols this way:

class MyTreeStore \< Gtk::TreeStore  
def initialize(\*args)  
@columns = args  
super(\*args)  
end

def   
if @columns[column] == Symbol  
super(column).to\_sym  
else  
super(column)  
end  
end  
end

This code will allow you to store symbols, and have them actually be  
returned as Symbols when fetching them. Storage isn’t an issue, as in  
order to store a Symbol in Gtk, it has to be converted to a String in  
the  
first place.

hth,

Mario

On Sat, Feb 23, 2013 at 2:36 PM, Quintus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> row[0] = “String”
> 
> Valete,  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Mario S.  
Fleet Captain  
CO - Geo 99  
CO - USS T’hy’la  
XO - Diplomatic Corps - Second Life  
[http://www.iftcommand.com/chapters/thyla/](http://www.iftcommand.com/chapters/thyla/)

> **[HOME | The Federation](https://www.trekfederation.com)**

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [February 24, 2013, 6:57am UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862/3 "2013-02-24T06:57:39Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] Bug in ListStore?” on Sat, 23 Feb 2013  
20:36:34 +0100,  
Quintus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> row[2] = 10
> 
> That is, the symbol has been converted to a string. Im not sure  
> whether this is a bug or expected behaviour, but if the latter, I was  
> unable to find it documented somewhere. Can someone light up this?

GObject doesn’t have symbol type. So Ruby/GLib2 maps Symbol  
to G\_TYPE\_STRING(\*).

(\*)

> **[GObject-2.0](https://docs.gtk.org/gobject/)**
>
> Reference for GObject-2.0

So you get string by the column that is specified as Symbol.

## Thanks,

kou

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [February 24, 2013, 9:34am UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862/4 "2013-02-24T09:34:12Z")

</div>

Am Sun, 24 Feb 2013 14:56:40 +0900 (JST)  
schrieb Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> So you get string by the column that is specified as Symbol.

Thank you kou :-). So this is kind of expected behaviour… However, I  
cant imagine GObject has a type for Ruby objects, which work as  
expected:

=============================================  
require “gtk2”

class Foo

def initialize  
@foo = 12  
end

end

model = Gtk::ListStore.new(String, Foo, Integer)  
row = model.append  
row[0] = “String”  
row[1] = Foo.new  
row[2] = 10

# row2 = model.iter\_first p [row[0], row[1], row[2]]

The Foo instance coming out here is exactly the same that I passed in  
(it even has the same object\_id), so it is possible to store arbitrary  
objects into ListStore. Perhaps it is possible to store the Symbol  
instance (i.e. the VALUE pointer rather than the ID it maps to) the  
same way? Im not sure how ID is defined, but I imagine it to be some  
kind of integral value, so another option could be storing it as an  
integer probably and converting that back on retrieval?

> ## Thanks,
> 
> kou

Vale,  
Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [February 24, 2013, 9:38am UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862/5 "2013-02-24T09:38:18Z")

</div>

Am Sat, 23 Feb 2013 22:02:52 -0500  
schrieb Mario S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hello Quintus,

Hi Mario,

> This code will allow you to store symbols, and have them actually be  
> returned as Symbols when fetching them. Storage isn’t an issue, as in  
> order to store a Symbol in Gtk, it has to be converted to a String in  
> the first place.

Thanks for your suggestion, I will use something like this for now. 🙂

> hth,
> 
> Mario

Vale,  
Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [February 24, 2013, 8:55am UTC](https://www.ruby-forum.com/t/bug-in-liststore/226862/6 "2013-02-24T08:55:04Z")

</div>

I had a similar problem where I wanted to make a subclass of the  
“String” class and then store it in a liststore:

class MyString \< String

def initialize(var)  
@var = var

end

end

When I store instances of this class in a liststore column, it is always  
converted into an instance of String not MyString. You can’t subclass  
Integer either.

It would be very very helpful if I could subclass these classes.

Thanks,  
Eric
