# Reference to an object

**URL:** <https://www.ruby-forum.com/t/reference-to-an-object/217584>\
**Category:** Ruby-Gnome 2\
**Created:** [March 5, 2012, 9:24pm UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584 "2012-03-05T21:24:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 5, 2012, 9:24pm UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/1 "2012-03-05T21:24:44Z")

</div>

I am trying to understand building an app using Glade and Ruby. I have  
a  
test app that has a top-level window with a fixed container. The  
container  
has three buttons on it. I would like to know how to reference those  
buttons in Ruby code to do simple things like disable or enable them  
using  
the sensitive attribute.

During the app startup I am keeping a reference to the window object via  
a  
builder using get\_object

window = builder.get\_object(“my\_window”)

# the following code works and disables all of the buttons

window.children.first.children.each do |obj|  
obj.sensitive = false  
end

However, I was looking for a way to reference the button object  
directly.

window.fixed\_container.button\_quit.sensitive = false

or at least something along those lines. Do I have to use the builder  
instance to get a reference to one of the buttons? I’m looking for the  
preferred way to accomplish this task.

Thanks!!

---

<div class="post-metadata">

**Author:** ![worthspending](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@worthspending](https://www.ruby-forum.com/u/worthspending)\
**Post date:** [March 5, 2012, 9:32pm UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/2 "2012-03-05T21:32:51Z")

</div>

Hi,

There are many open-source Ruby Gtk+ apps for you to browse the source  
code.

[http://launchpad.net/studly](http://launchpad.net/studly) (flashcard trainer)  
[ScreenRuler in Launchpad](http://launchpad.net/screenruler) (on-screen ruler)  
[Luz in Launchpad](http://launchpad.net/luz) (motion graphics editor)

> However, I was looking for a way to reference the button object directly.
> 
> window.fixed\_container.button\_quit.sensitive = false

You can achieve this by mixing Ruby’s attr\_accessor feature with the  
array of widgets symbols in any of those above apps. See  
glade\_window.rb in Studly for example.

Though I recommend you don’t do this, and instead do  
window.set\_some\_state and let that method configure the window.

-Ian

---

<div class="post-metadata">

**Author:** ![worthspending](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@worthspending](https://www.ruby-forum.com/u/worthspending)\
**Post date:** [March 6, 2012, 12:01am UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/3 "2012-03-06T00:01:00Z")

</div>

Hi:

Ian’s code is a great resource. He’s helped me learn many things in  
ruby.

I’ll give you a few basics:

> window = builder.get\_object(“my\_window”)

This can be abbreviated to this:

window = builder[“my\_window”]

and to set the title, for example, you could just use:

builder[“my\_window”].title = “Title appears at top of window”

or

builder[“button1”].sensitive = false

Normally, you’d want “builder” to be an instance variable @builder  
instead, so you can use it everywhere in your class. I’ve written an  
IDE for ruby that uses that exact variable name, @builder to do exactly  
what you’re trying to do. You can see simple examples at  
[visualruby.net](http://visualruby.net).

Good luck,  
Eric

---

<div class="post-metadata">

**Author:** ![worthspending](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@worthspending](https://www.ruby-forum.com/u/worthspending)\
**Post date:** [March 6, 2012, 12:32am UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/4 "2012-03-06T00:32:39Z")

</div>

Thanks guys!! I picked up some very good ideas…

On Mon, Mar 5, 2012 at 5:01 PM, Eric C.

---

<div class="post-metadata">

**Author:** ![worthspending](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@worthspending](https://www.ruby-forum.com/u/worthspending)\
**Post date:** [March 6, 2012, 2:38am UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/5 "2012-03-06T02:38:49Z")

</div>

hi W.W. -

> … I was looking for a way to reference the button object  
> directly.
> 
> window.fixed\_container.button\_quit.sensitive = false

i don’t use glade, and the builder suggestions above are great - but  
couldn’t you just use an instance variable to reference the buttons? i  
often do something like this:

require ‘gtk2’

class Nothing  
def initialize  
win = Gtk::Window.new  
win.signal\_connect(“destroy”){Gtk.main\_quit}  
box = Gtk::HBox.new(true, 2)  
button1 = Gtk::Button.new(“click me and see”)  
button1.signal\_connect(“clicked”){button1\_click\_event}  
@button2 = Gtk::Button.new(“I’m just a pawn in this game”)  
@button2.signal\_connect(“clicked”){p “I’m button2, and I’m  
feeling sensitive”}  
box.pack\_start(button1, true, true, 2)  
box.pack\_start(@button2, true, true, 2)  
win.add(box)  
win.show\_all  
@switch\_button2 = false  
end

def button1\_click\_event  
p “I’m the master of this game!”  
@button2.sensitive = @switch\_button2  
@switch\_button2 = !@switch\_button2  
end  
end

n = Nothing.new  
Gtk.main

- j

---

<div class="post-metadata">

**Author:** ![worthspending](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@worthspending](https://www.ruby-forum.com/u/worthspending)\
**Post date:** [March 6, 2012, 12:16pm UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/6 "2012-03-06T12:16:15Z")

</div>

hi Eric,

> take a look at [visualruby.net](http://visualruby.net)

i will, looks interesting - definitely less clutter in the code…

- j

---

<div class="post-metadata">

**Author:** ![worthspending](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@worthspending](https://www.ruby-forum.com/u/worthspending)\
**Post date:** [March 6, 2012, 6:10am UTC](https://www.ruby-forum.com/t/reference-to-an-object/217584/7 "2012-03-06T06:10:06Z")

</div>

Hi Jake:

I’d encourage you to take a look at a program that I just made  
available,  
visualruby. It can really streamline your code. For example all the  
code that you wrote above could be reduced to this:

class Nothing

def initialize  
@button1 = “click me and see”  
@button2 = “I’m just a pawn in this game”  
load\_glade( **FILE** )  
set\_glade\_variables  
@switch\_button2 = false  
show\_window()  
end

def button2\_\_clicked  
p “I’m button2, and I’m feeling sensitive”  
end

def button1\_\_clicked  
p “I’m the master of this game!”  
@builder[“button2”].sensitive = !@switch\_button2  
@switch\_button2 = !@switch\_button2  
end

end

And if you set the values of button1 and button2 in glade, and fixed  
button1\_clicked:

class Nothing

include GLadeGUI

def initialize  
load\_glade( **FILE** )  
show\_window()  
end

def button2\_\_clicked  
p “I’m button2, and I’m feeling sensitive”  
end

def button1\_\_clicked  
p “I’m the master of this game!”  
@builder[“button2”].sensitive = !@builder[“button2”].sensitive  
end

end

I know that your code works, but using visualruby and glade will  
unclutter it.

take a look at [visualruby.net](http://visualruby.net)

Best,  
Eric
