tobbyd
September 30, 2007, 6:07pm
1
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
tobbyd
September 30, 2007, 7:09pm
2
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
tobbyd
October 1, 2007, 6:34pm
3
I think you forgot a = there detlef
@buttons [“button#{i}”] = Gtk::Button.new(“button #{i}”)
tobbyd
October 1, 2007, 10:18pm
4
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
tobbyd
October 2, 2007, 11:07am
5
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/ref_c_hash.html
martin
tobbyd
October 2, 2007, 12:47pm
6
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