# User data in callback functions

**URL:** <https://www.ruby-forum.com/t/user-data-in-callback-functions/71321>\
**Category:** Ruby-Gnome 2\
**Created:** [October 6, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321 "2006-10-06T23:01:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 6, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/1 "2006-10-06T23:01:22Z")

</div>

Hello Ruby-Gnome2 users and developers!

I’ve been making an application using Ruby-Gtk2 for the past few days  
or so, and now I’ve run into a problem. I’m trying to pass user data  
into a signal callback function, but I can’t figure out how to do  
that.

I have a loop with object.signal\_connect( “clicked” ) { puts variable  
}. The `variable' changes with every cycle of the loop. The problem is, my signal handler function's `variable’ seems to be the wrong one;  
it’s the value that was last stored into it. This, of course, produces  
really bad results.

Here’s a simple test case I made:

# START

#!/usr/bin/ruby  
require ‘gtk2’

Gtk.init()

vbox = Gtk::VBox.new()  
for i in 0…5  
button = Gtk::Button.new( “Button #” + i.to\_s )  
button.signal\_connect( “clicked” ) { puts "clicked " + i.to\_s }  
vbox.pack\_start( button, true, true, 0 )  
end

window = Gtk::Window.new()  
window.signal\_connect( “destroy” ) { Gtk.main\_quit }  
window.add( vbox )  
window.show\_all()

Gtk.main()

# END

Alternatively you can wget that from [http://tohveli.net/example.rb](http://tohveli.net/example.rb)

The C version of GTK+ offers a chance to pass the user data as a  
parameter in signal\_connect  
([http://developer.gnome.org/doc/API/2.0/gobject/gobject-Signals.html#g-signal-connect](http://developer.gnome.org/doc/API/2.0/gobject/gobject-Signals.html#g-signal-connect)),  
but this doesn’t seem to be the case with Ruby-Gnome2.

Is there any way to make this work, to pass user data into a callback  
function? It seems I can’t even use the user-data property in  
Gtk::Object, as the documentation says it’s unaccessible from Ruby!  
([http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3AObject#user-data%3A+GLib%3A%3APointer](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3AObject#user-data%3A+GLib%3A%3APointer))

Any help is greatly appreciated.

Thanks,  
– Juha Varkki

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 6, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/2 "2006-10-06T23:27:24Z")

</div>

try this one:

Juha Varkki wrote:

> it’s the value that was last stored into it. This, of course, produces  
> for i in 0…5  
> button = Gtk::Button.new( “Button #” + i.to\_s )  
> button.signal\_connect( “clicked” ) { puts "clicked " + i.to\_s }

button.signal\_connect( “clicked” ) {|me| puts "clicked " + me.label.to\_s  
}

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 6, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/3 "2006-10-06T23:33:26Z")

</div>

Hi,

Umm, no. The variable I need is not stored as a label or anything in  
the widget. It’s totally separate data. That’s why I mentioned about  
the user-data property in Gtk::Object, which I can’t use for some  
reason.

– Juha Varkki

2006/10/7, Dobai-Pataky B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > I have a loop with object.signal\_connect( “clicked” ) { puts variable  
> > Gtk.init()  
> > end
> > 
> > Any help is greatly appreciated.  
> > ruby-gnome2-devel-en mailing list
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 6, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/4 "2006-10-06T23:35:26Z")

</div>

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 6, 2006, 11:43pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/5 "2006-10-06T23:43:27Z")

</div>

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 7, 2006, 5:35am UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/6 "2006-10-07T05:35:33Z")

</div>

Hi,

In [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
“[ruby-gnome2-devel-en] User data in callback functions” on Fri, 6 Oct  
2006 23:59:29 +0300,  
“Juha Varkki” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a loop with object.signal\_connect( “clicked” ) { puts variable  
> }. The `variable' changes with every cycle of the loop. The problem is, my signal handler function's `variable’ seems to be the wrong one;  
> it’s the value that was last stored into it. This, of course, produces  
> really bad results.

What about the following:

button.signal\_connect( “clicked”, i ) do |widget, var|  
puts "clicked " + var.to\_s  
end

> end
> 
> window = Gtk::Window.new()  
> window.signal\_connect( “destroy” ) { Gtk.main\_quit }  
> window.add( vbox )  
> window.show\_all()
> 
> Gtk.main()

## Thanks,

kou

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 7, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/7 "2006-10-07T16:40:16Z")

</div>

Ah! Thank you Kouhei! That’s exactly what I was looking for. I wonder  
why that isn’t documented.

e$B$“$j$,$H$&$4$6$$$^$7$?!”$3$&$X$$$5$s!\*e(B

– Juha Varkki

2006/10/7, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > really bad results.
> > 
> > window.signal\_connect( “destroy” ) { Gtk.main\_quit }  
> > Take Surveys. Earn Cash. Influence the Future of IT  
> > Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share your  
> > opinions on IT & business topics through brief surveys – and earn cash  
> > [http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 6, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/8 "2006-10-06T23:37:27Z")

</div>

I see. Ruby-Gnome2 definitely doesn’t have all the necessary functions  
to do GTK+ programming in Ruby yet.

Is it planned to add (an optional) user data parameter to the  
signal\_connect function in Ruby-Gnome2? That’d make everything whole  
lot of easier for everyone.

– Juha Varkki

2006/10/7, Dobai-Pataky B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> reason.
> 
> it’s the value that was last stored into it. This, of course, produces  
> for i in 0…5
> 
> The C version of GTK+ offers a chance to pass the user data as a
> 
> removed\_email\_address@domain.invalid  
> ruby-gnome2-devel-en mailing list  
> opinions on IT & business topics through brief surveys – and earn cash  
> opinions on IT & business topics through brief surveys – and earn cash  
> [http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> removed\_email\_address@domain.invalid  
> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)

---

<div class="post-metadata">

**Author:** ![Juha\_Varkki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Juha\_Varkki](https://www.ruby-forum.com/u/Juha_Varkki)\
**Post date:** [October 10, 2006, 4:33am UTC](https://www.ruby-forum.com/t/user-data-in-callback-functions/71321/9 "2006-10-10T04:33:26Z")

</div>

Hi,

> Ah! Thank you Kouhei! That’s exactly what I was looking for. I wonder  
> why that isn’t documented.

I updated the document.

> > end
> > 
> > > ```
> > > button.signal_connect( "clicked" ) { puts "clicked " + i.to_s }
> > > vbox.pack_start( button, true, true, 0 )
> > > 
> > > ```
> > > 
> > > end
> > > 
> > > window = Gtk::Window.new()  
> > > window.signal\_connect( “destroy” ) { Gtk.main\_quit }  
> > > window.add( vbox )  
> > > window.show\_all()
> > > 
> > > Gtk.main()

## Thanks,

kou

* * *

Take Surveys. Earn Cash. Influence the Future of IT  
Join [SourceForge.net](http://SourceForge.net)’s Techsay panel and you’ll get the chance to share  
your  
opinions on IT & business topics through brief surveys – and earn cash  
[http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV](http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV)
