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?
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.
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.
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?
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.