# Dynamically create buttons

**URL:** <https://www.ruby-forum.com/t/dynamically-create-buttons/115939>\
**Category:** Ruby-Gnome 2\
**Created:** [September 30, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939 "2007-09-30T18:07:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [September 30, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939/1 "2007-09-30T18:07:38Z")

</div>

I am a beginner and I would like to create a loop that dynamically  
creates a number of normal buttons. ‘Button1’, ‘Button2’ etc.

Then I would like to be able to reference the dynamically created  
buttons by typing ‘Button1’, ‘Button2’ etc

I would be very thankful if someone could help me out with this code  
snippet.

Thank you

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [September 30, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939/2 "2007-09-30T19:09:27Z")

</div>

Am Sonntag, den 30.09.2007, 18:07 +0200 schrieb tobbyd dubbyd:

> I am a beginner and I would like to create a loop that dynamically  
> creates a number of normal buttons. ‘Button1’, ‘Button2’ etc.
> 
> Then I would like to be able to reference the dynamically created  
> buttons by typing ‘Button1’, ‘Button2’ etc

Hi,

something like this?

@buttons = {}  
10.times do |i|  
@buttons[“button#{i}”] Gtk::Button.new(“button #{i}”)  
end

Cheers  
detlef

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 1, 2007, 6:34pm UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939/3 "2007-10-01T18:34:17Z")

</div>

I think you forgot a = there detlef 🙂

@buttons[“button#{i}”] = Gtk::Button.new(“button #{i}”)

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 1, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939/4 "2007-10-01T22:18:45Z")

</div>

Marc H. wrote:

> I think you forgot a = there detlef 🙂
> 
> @buttons[“button#{i}”] = Gtk::Button.new(“button #{i}”)

Fantastic work! It runs perfectly. I am a bit unsure of exactly what is  
happening here, what is -\> @buttons = {}  
Is it some sort of array that all the buttons are stored in?

Thanks alot

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 2, 2007, 11:07am UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939/5 "2007-10-02T11:07:05Z")

</div>

tobbyd dubbyd wrote:

> … I am a bit unsure of exactly what is  
> happening here, what is → @buttons = {}  
> Is it some sort of array that all the buttons are stored in?

Yes, it’s called a Hash, and it’s much more than an array.  
You can learn about them here:

[http://www.rubycentral.com/pickaxe/tut\_containers.html](http://www.rubycentral.com/pickaxe/tut_containers.html)  
[http://www.rubycentral.com/pickaxe/ref\_c\_hash.html](http://www.rubycentral.com/pickaxe/ref_c_hash.html)

martin

---

<div class="post-metadata">

**Author:** ![tobbyd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tobbyd](https://www.ruby-forum.com/u/tobbyd)\
**Post date:** [October 2, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/dynamically-create-buttons/115939/6 "2007-10-02T12:47:19Z")

</div>

tobbyd dubbyd wrote:

> Fantastic work! It runs perfectly. I am a bit unsure of exactly what is  
> happening here, what is -\> @buttons = {}

@buttons = {}  
is the shortcut to  
@buttons = Hash.new

This works also for arrays  
a = []  
does the same as  
a = Array.new
