Bug in ListStore?

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

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 [email protected]
wrote:

row[0] = “String”

Valete,
[email protected]
ruby-gnome2-devel-en List Signup and Options


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

Hi,

In [email protected]
“[ruby-gnome2-devel-en] Bug in ListStore?” on Sat, 23 Feb 2013
20:36:34 +0100,
Quintus [email protected] 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(*).

(*)

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

Thanks,

kou

Am Sun, 24 Feb 2013 14:56:40 +0900 (JST)
schrieb Kouhei S. [email protected]:

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

Am Sat, 23 Feb 2013 22:02:52 -0500
schrieb Mario S. [email protected]:

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. :slight_smile:

hth,

Mario

Vale,
Marvin

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