Forum: Ruby-Gnome 2 Usage of symbol instead of string for signal names ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5508868cfffa79d0975bee50167af846?d=identicon&s=25 eclipse survey (Guest)
on 2007-02-08 18:36
(Received via mailing list)
Hi,

  I'm just wondering why strings are used for signal names.
  What I have learnt from Ruby is that symbols are more efficient than
strings.
  Symbols are typically used for keys in a hash ...

  Anyway, Ruby/GTK2 is great.

  CM.
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2007-02-08 18:43
(Received via mailing list)
On 2/8/07, eclipse survey <eclipse.survey@yahoo.com> wrote:

> I'm just wondering why strings are used for signal names.
> What I have learnt from Ruby is that symbols are more efficient than
> strings.
> Symbols are typically used for keys in a hash ...

Symbols are also strings that are never freed.  And the GObject way of
sending signals is by passing strings around anyway, so we're really
not saving ourselves much by using symbols instead.

  nikolai

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
5508868cfffa79d0975bee50167af846?d=identicon&s=25 eclipse survey (Guest)
on 2007-02-09 11:54
(Received via mailing list)
Nikolai,

  From the Ruby side :
  - for a given string literal, there is only one corresponding symbol.
  - if you call 3 times signal_connect("a_signal") you create 3
different strings with the same value of "a_signal"
  - Conversely, if you call 3 times signal_connect(:a_signal) you refer
to the same instance of symbol a_signal
  - Most of signal names correspond to GTK events, so the fact that a
symbol is a string that is never freed and exists only once (for a given
value) is a serious advantage.

  I don't know how you can take advantage of this in the context of
GTK/GLIB.

  CM.

Nikolai Weibull <now@bitwi.se> wrote:
  On 2/8/07, eclipse survey wrote:

> I'm just wondering why strings are used for signal names.
> What I have learnt from Ruby is that symbols are more efficient than
> strings.
> Symbols are typically used for keys in a hash ...

Symbols are also strings that are never freed. And the GObject way of
sending signals is by passing strings around anyway, so we're really
not saving ourselves much by using symbols instead.

nikolai

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
9fe908f5593444a40be14a424c372e58?d=identicon&s=25 Masao Mutoh (Guest)
on 2007-02-09 14:18
(Received via mailing list)
Hi,

On Thu, 8 Feb 2007 09:35:37 -0800 (PST)
eclipse survey <eclipse.survey@yahoo.com> wrote:

> Hi,
>
>   I'm just wondering why strings are used for signal names.

Did you try it?
----------------------
require 'gtk2'

button = Gtk::Button.new("click")
button.signal_connect(:clicked) do
  p "Hello World"
end
win = Gtk::Window.new.set_default_size(100, 100)
win.add(button).show_all
win.signal_connect(:destroy) do
  p "destroy"
  Gtk.main_quit
end
Gtk.main
----------------------

>   What I have learnt from Ruby is that symbols are more efficient than strings.
>   Symbols are typically used for keys in a hash ...

In Ruby-GNOME2, symbol is converted to char* to call a GLib function.
#Then the char* string is converted to guint and GQuark by GLib.

So it's not efficient way to use symbols instead of strings.
That's the reason why I don't recommand to use symbols in this cases.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2007-02-09 15:08
(Received via mailing list)
On 2/9/07, eclipse survey <eclipse.survey@yahoo.com> wrote:

Can you please quote my response in a more mail-friendly manner?

> - if you call 3 times signal_connect("a_signal") you create 3 different
> strings with the same value of "a_signal"

If you're worried about creating strings you can always create a
constant.

> - Most of signal names correspond to GTK events, so the fact that a symbol
> is a string that is never freed and exists only once (for a given value) is
> a serious advantage.

Why?  Just to make my point about symbols absolutely clear: a symbol
will /never/ be freed after it's been created during the execution of
the program.

As Masao already pointed out, symbols don't really save you anything.

  nikolai

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
5508868cfffa79d0975bee50167af846?d=identicon&s=25 eclipse survey (Guest)
on 2007-02-12 09:57
(Received via mailing list)
>Nikolai Weibull <now@bitwi.se> wrote:

  >Can you please quote my response in a more mail-friendly manner?

  Sorry. I think it's better now.

  > If you're worried about creating strings you can always create a
constant.

  You have answered to my concern : - ) !

  Is it possible to have within Ruby/GTK all the string constants used
by "standard" GTK signals. It would be great.

  CM.
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2007-02-12 14:17
(Received via mailing list)
On 2/12/07, eclipse survey <eclipse.survey@yahoo.com> wrote:
> >Nikolai Weibull <now@bitwi.se> wrote:
>
> >Can you please quote my response in a more mail-friendly manner?
>
> Sorry. I think it's better now.

Yes, thank you.

> > If you're worried about creating strings you can always create a constant.
>
> You have answered to my concern : - ) !
>
> Is it possible to have within Ruby/GTK all the string constants used by
> "standard" GTK signals. It would be great.

Well, the whole reason for using string constants is so that one
doesn't have to commit to some specific scheme, and strings are more
unique than integers.  But no, there's no way to get the string
constants used by Gtk.  And there's really no reason for doing this.
If you're worrying about these kinds of things, then you may want to
consider all the layers of code that a call to a Ruby/Gtk method goes
through.  It's a lot more than the creation of a string.

  nikolai

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
5508868cfffa79d0975bee50167af846?d=identicon&s=25 eclipse survey (Guest)
on 2007-02-12 16:55
(Received via mailing list)
Nikolai Weibull <now@bitwi.se> wrote:

  >> > If you're worried about creating strings you can always create a
constant.
>>
>> You have answered to my concern : - ) !
>>
>> Is it possible to have within Ruby/GTK all the string constants used by
>> "standard" GTK signals. It would be great.

>Well, the whole reason for using string constants is so that one
>doesn't have to commit to some specific scheme, and strings are more
>unique than integers. But no, there's no way to get the string
>constants used by Gtk. And there's really no reason for doing this.
>If you're worrying about these kinds of things, then you may want to
>consider all the layers of code that a call to a Ruby/Gtk method goes
>through. It's a lot more than the creation of a string.

  >nikolai

I think there is a misunderstanding here. What I suggest/request is only
having constants defined like this :

  module Gtk
    BUTTON_PRESS_EVENT = 'button_press_event'
    BUTTON_RELEASE_EVENT = 'button_release_event'
    BUTTON_SCROLL_EVENT = 'scroll_event'
    # etc. for other standard GTK events
  end

  Thus, one can use the constant variables instead of specifying
directly a string and consequently save the creation of a string each
time.

  CM.
3807680355002d0d5e8314a97333587e?d=identicon&s=25 Nikolai Weibull (Guest)
on 2007-02-12 17:09
(Received via mailing list)
On 2/12/07, eclipse survey <eclipse.survey@yahoo.com> wrote:

> Thus, one can use the constant variables instead of specifying directly a
> string and consequently save the creation of a string each time.

Yes, that was what I was suggesting, but it's also not something I
recommend.  Especially as the events use '-', not '_', as separators.
That is, it's called 'button-press-event'

  nikolai

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
9fe908f5593444a40be14a424c372e58?d=identicon&s=25 Masao Mutoh (Guest)
on 2007-02-12 17:35
(Received via mailing list)
Hi,

On Mon, 12 Feb 2007 07:54:50 -0800 (PST)
eclipse survey <eclipse.survey@yahoo.com> wrote:

> >doesn't have to commit to some specific scheme, and strings are more
>   module Gtk
>     BUTTON_PRESS_EVENT = 'button_press_event'
>     BUTTON_RELEASE_EVENT = 'button_release_event'
>     BUTTON_SCROLL_EVENT = 'scroll_event'
>     # etc. for other standard GTK events
>   end

Ruby/GTK(1) was supported these constants.

e.g.)
button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
   p "Hello World"
}

But when Ruby/GTK2 was started, we discussed and decided to remove that
feature.

button.signal_connect("clicked") {
   p "Hello World"
}

This is simpler than Ruby/GTK(1) style and I prefer this.

>   Thus, one can use the constant variables instead of specifying directly a string and 
consequently save the creation of a string each time.

If you really need to save the creation time, you can define them in
your own application/library.
But if you really need to save the trivial time, I recommand to use C
language to implement your application.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
5508868cfffa79d0975bee50167af846?d=identicon&s=25 eclipse survey (Guest)
on 2007-02-13 11:21
(Received via mailing list)
Masao Mutoh <mutoh@highway.ne.jp> wrote:

  >Ruby/GTK(1) was supported these constants.
>e.g.)
>button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
>p "Hello World"
>}
>
>But when Ruby/GTK2 was started, we discussed and decided to remove that feature.
  >
  >button.signal_connect("clicked") {
>p "Hello World"
>}

  It's a pity. Having these constants defined doesn' force you to use
them. So you can still use directly string.
  I agree that I can define these constants in my own application. But
other people may define their own constants as well with different
naming rules ... I'm just thinking about convention that "eases" other
people get into my code ...
  In the standard library, there is the English library that gives a
less cryptic names to global symbols. The aim is different but quite
similar as having those constants. (I don't say that "clicked" is
cryptic). You can use the English names for the global names.

  "clicked" is definitively simpler than Gtk::Button::SIGNAL_CLICKED.
  The naming scheme could be simpler as well like Gtk::BUTTON_CLICKED.
  All the constants can be defined directly in the Gtk module.

  CM.
76e45b80a4f6b14bb3cc8b85079792d6?d=identicon&s=25 Emmanuel Touzery (Guest)
on 2007-02-13 11:27
(Received via mailing list)
Hello,

eclipse survey wrote:
> >But when Ruby/GTK2 was started, we discussed and decided to remove
> that feature.
> >
> >button.signal_connect("clicked") {
> >p "Hello World"
> >}
> It's a pity. Having these constants defined doesn' force you to use
> them. So you can still use directly string.
A problem is also that someone must maintain the list of constants
up-to-date.
I think the problem is that you don't provide an argument which is good
enough to justify the work.
If we talk about speed and memory use, those constants actually make the
program slightly less efficient.

Then using it is only a matter of taste. That probably doesn't justify
the burden of maintaining them.

emmanuel

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
5508868cfffa79d0975bee50167af846?d=identicon&s=25 eclipse survey (Guest)
on 2007-02-13 12:01
(Received via mailing list)
Hi,

Emmanuel Touzery <emmanuel.touzery@free.fr> wrote:

  >A problem is also that someone must maintain the list of constants
>up-to-date.

  I agree.

  >If we talk about speed and memory use, those constants actually make
the
>program slightly less efficient.

  I don't understand why ? Am I missing something ?
  I do understand that a symbol is not a good idea for this issue given
the underlying GTK/GLIB.

  But I don't understant why a constant refering to a string is less
efficient in speed and in memory. Conversely, I think that it saves the
cost of creating each time a new string.

  CM.
76e45b80a4f6b14bb3cc8b85079792d6?d=identicon&s=25 Emmanuel Touzery (Guest)
on 2007-02-13 12:12
(Received via mailing list)
Hello,

> >If we talk about speed and memory use, those constants actually make the
> >program slightly less efficient.
>
> I don't understand why ? Am I missing something ?
> I do understand that a symbol is not a good idea for this issue
> given the underlying GTK/GLIB.
>
> But I don't understant why a constant refering to a string is less
> efficient in speed and in memory. Conversely, I think that it saves
> the cost of creating each time a new string.
OK, you're right, actually constants (unlike symbols) don't make the
program less speed-efficient. Though they probably take up memory even
if you don't use them. But as Nikolai Weibull pointed out, this is
saving 0.001% of the runtime speed or memory use here.
So I really think it's not worth the effort, unless you can make a
benchmark that shows a measurable speed difference... which would
surprise me a lot.

emmanuel

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your job
easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709...
This topic is locked and can not be replied to.