# Using visualruby to edit a Hash

**URL:** <https://www.ruby-forum.com/t/using-visualruby-to-edit-a-hash/237934>\
**Category:** Ruby-Gnome 2\
**Created:** [December 6, 2013, 7:12pm UTC](https://www.ruby-forum.com/t/using-visualruby-to-edit-a-hash/237934 "2013-12-06T19:12:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [December 6, 2013, 7:12pm UTC](https://www.ruby-forum.com/t/using-visualruby-to-edit-a-hash/237934/1 "2013-12-06T19:12:11Z")

</div>

I am new to visualruby. I am looking for a way to display and alter the  
contents of a Hash. I have code that uses VRuby from ruby 1.8.7 that  
would take a hash and dynamically display a dialog with drop-downs,  
tree-lists, edit boxes, check boxes, buttons, etc. depending of the type  
of the hash key,value entry.  
I have been experimenting with VR::ListView, but is seems that I can  
only have one display type per column.  
Is there perhaps a way to dynamically add items(widgets?) to a Glade  
form?

For example how could I display and gather values from a window similar  
to this post form?

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [December 6, 2013, 8:11pm UTC](https://www.ruby-forum.com/t/using-visualruby-to-edit-a-hash/237934/2 "2013-12-06T20:11:43Z")

</div>

Hi:

One way to add widgets to a glade form is to place empty vbox and hbox  
widgets to your form using the glade interface designer, then fill them  
in later with your code.

For example, add a vbox to your form with 1 empty row. You can keep the  
name “vbox1”. Later, in your code, you can do this:

@dropdown = VR::SimpleComboBoxEntry(“choice”, “choice2”)  
@builder[“vbox1”].add(@dropdown)

Then the vbox1 widget will be filled with the dropdown entry widget.

Currently, the way you’d update your hash is by putting a submit button  
on the form, and making a method to handle the click event. You make  
the method name with the naming convention with two underscores. If you  
had a submit button called “submitButton” the method would be:

def submitButton\_\_clicked(\*args)  
@hash[“name”] = @builder[“nameWidget”]  
etc…  
end

If you wanted to dynamically change each field when the widget changed  
value, It would be something like this:

def nameWidget\_\_changed(\*args)  
@hash[“name”] = @builder[“nameWidget”]  
end

Does this help?

Eric

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [December 6, 2013, 8:51pm UTC](https://www.ruby-forum.com/t/using-visualruby-to-edit-a-hash/237934/3 "2013-12-06T20:51:20Z")

</div>

Thanks for the reply. I would like to be able to build the form with a  
variable number of entries.

The first example (adapted from one of the samples) was a ListView with  
rows for each hash entry.  
When I click on the value column of each row the appropriate widget is  
displayed (editbox, dropdown, etc.) where I can alter the value. This  
works, but I would like to find a way to do it all (or at least most of  
it) on one form.

I think that I understand using vbox1 and adding a widget to it. I  
think that it is directly covered in one of the examples.

Can I add vbox’s to an existing glade form at run time?  
Or can I add multiple widgets via @builder[“vbox1”].add(@xxx)  
@builder[“vbox1”].add(@yyy)?

Or can I have a max number of vbox place holders in the form and hide  
them if I don’t need them? Assuming they would take no space in the  
form if they aren’t used.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [December 6, 2013, 11:24pm UTC](https://www.ruby-forum.com/t/using-visualruby-to-edit-a-hash/237934/4 "2013-12-06T23:24:01Z")

</div>

Hi Hans:

I beleive that you can do all the things that you described in your last  
post. Gtk is made so you can add widgets to containers (see pack\_start  
etc.) However, this is a Gtk issue not a visualruby issue. Remember  
that visualruby is just working with Gtk objects in the same way that  
you do in your code. You will need to get a reference to the gtk  
object:

@contain = @builder[“myBox”]  
@container.add(…)

You will need to consult the ruby-gnome docs. My goal is to simplify  
all of this.

I think you’re headed in the right direction.

I’m trying to map data types directly to widgets, so when you write  
code, you don’t even have to worry about how they are displayed. For  
example, a string in your code would map to a label or a textbox etc.

Eric
