# Usage of symbol instead of string for signal names?

**URL:** <https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929>\
**Category:** Ruby-Gnome 2\
**Created:** [February 8, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929 "2007-02-08T18:36:46Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 8, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/1 "2007-02-08T18:36:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 8, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/2 "2007-02-08T18:43:47Z")

</div>

On 2/8/07, eclipse survey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 9, 2007, 11:54am UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/3 "2007-02-09T11:54:21Z")

</div>

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 W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 9, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/4 "2007-02-09T15:08:39Z")

</div>

On 2/9/07, eclipse survey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 9, 2007, 2:18pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/5 "2007-02-09T14:18:35Z")

</div>

Hi,

On Thu, 8 Feb 2007 09:35:37 -0800 (PST)  
eclipse survey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 12, 2007, 9:57am UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/6 "2007-02-12T09:57:34Z")

</div>

> Nikolai W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 12, 2007, 2:17pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/7 "2007-02-12T14:17:02Z")

</div>

On 2/12/07, eclipse survey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Nikolai W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 12, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/8 "2007-02-12T16:55:25Z")

</div>

Nikolai W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 12, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/9 "2007-02-12T17:09:28Z")

</div>

On 2/12/07, eclipse survey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 12, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/10 "2007-02-12T17:35:32Z")

</div>

Hi,

On Mon, 12 Feb 2007 07:54:50 -0800 (PST)  
eclipse survey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 13, 2007, 11:21am UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/11 "2007-02-13T11:21:41Z")

</div>

Masao M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 13, 2007, 11:27am UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/12 "2007-02-13T11:27:42Z")

</div>

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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 13, 2007, 12:12pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/13 "2007-02-13T12:12:46Z")

</div>

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 W. 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&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![eclipse\_survey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8491ac/32.png) [@eclipse\_survey](https://www.ruby-forum.com/u/eclipse_survey)\
**Post date:** [February 13, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/usage-of-symbol-instead-of-string-for-signal-names/84929/14 "2007-02-13T12:01:44Z")

</div>

Hi,

Emmanuel T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
