# Signal\_emit for other object

**URL:** <https://www.ruby-forum.com/t/signal-emit-for-other-object/151812>\
**Category:** Ruby-Gnome 2\
**Created:** [October 26, 2008, 3:30am UTC](https://www.ruby-forum.com/t/signal-emit-for-other-object/151812 "2008-10-26T03:30:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@cyrill](https://www.ruby-forum.com/u/cyrill)\
**Post date:** [October 26, 2008, 3:30am UTC](https://www.ruby-forum.com/t/signal-emit-for-other-object/151812/1 "2008-10-26T03:30:26Z")

</div>

my code:  
[http://pastie.org/300627](http://pastie.org/300627)  
Why generated event, in same object, can not be heard in other?  
Sorry for my english:)

---

<div class="post-metadata">

**Author:** ![cyrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@cyrill](https://www.ruby-forum.com/u/cyrill)\
**Post date:** [October 29, 2008, 8:24am UTC](https://www.ruby-forum.com/t/signal-emit-for-other-object/151812/2 "2008-10-29T08:24:23Z")

</div>

Cyrill Jakovlev wrote:

> my code:  
> [http://pastie.org/300627](http://pastie.org/300627)  
> Why generated event, in same object, can not be heard in other?  
> Sorry for my english:)

I think you are missing the “type\_register” command.

Here’s the pattern that I’ve used successfully:

class Foo \< Gtk::Object  
type\_register  
CHANGED = ‘changed’  
signal\_new(CHANGED,  
GLib::Signal::RUN\_FIRST, # flags  
nil, # accumulator (XXX: not  
supported yet)  
nil, # return type (void == nil)  
Hash # parameter types: Hash  
)

# needed to be able to emit the “changed” signal

def signal\_do\_changed(params)  
end

def something\_useful  
…  
params ||= {}  
signal\_emit(CHANGED, params)  
end  
end

class Bar  
…  
foo = Foo.new  
foo.signal\_connect(Foo::CHANGED) {|obj, params|  
on\_foo\_changed(params)}  
…  
end

I’ve used this pattern for several widgets and EventBox objects. Just  
inherit from correct object. For example: class Foo \< Gtk::EventBox

HTH,  
Roy

---

<div class="post-metadata">

**Author:** ![cyrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@cyrill](https://www.ruby-forum.com/u/cyrill)\
**Post date:** [October 29, 2008, 11:37am UTC](https://www.ruby-forum.com/t/signal-emit-for-other-object/151812/3 "2008-10-29T11:37:19Z")

</div>

mm… no  
I don’t make type\_register, because I refined class GLib::Object. In  
GLib::Object type\_register called before…  
Your example can hear signal only inside object. If I call  
signal\_emit(‘changed’), other instances of class Foo (and all other  
objects) wouldn’t know about it.  
I want to all object (instance classes extends GLib::Object) can hear  
signal, and I find solution: [http://pastie.org/301012](http://pastie.org/301012)  
signal\_emit from button call method from window. How u like this?)

---

<div class="post-metadata">

**Author:** ![cyrill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3bc359/32.png) [@cyrill](https://www.ruby-forum.com/u/cyrill)\
**Post date:** [October 29, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/signal-emit-for-other-object/151812/4 "2008-10-29T21:23:27Z")

</div>

Cyrill Jakovlev wrote:

> mm… no  
> I don’t make type\_register, because I refined class GLib::Object. In  
> GLib::Object type\_register called before…  
> Your example can hear signal only inside object. If I call  
> signal\_emit(‘changed’), other instances of class Foo (and all other  
> objects) wouldn’t know about it.  
> I want to all object (instance classes extends GLib::Object) can hear  
> signal, and I find solution: [http://pastie.org/301012](http://pastie.org/301012)  
> signal\_emit from button call method from window. How u like this?)

OK, you are wanting truly global signals. First, I’d suggest removing  
the instance signal\_connect (line 15) and signal\_emit (line 19) in your  
example. By removing them, GeneralWindow and ToolsPanel will each  
receive only one signal via signal\_connect\_from\_all\_objects. So you end  
up with a pure global signal concept that is not blurred with normal  
signal handling.

Also I’d suggest adding a signal\_disconnect\_from\_all\_objects method to  
Glib::Object. I’d suspect that you could run into problems deleting and  
garbage collecting objects without it.

Part of me likes the global signal pattern, while another part of me has  
concerns that the pattern can be abused leading to some difficult to  
debug problems.

Thanks for sharing.

Have fun,  
Roy
