From: "Brian Schröder" Date: 2004-12-09T20:43:04+09:00 Subject: Re: binding question On Thu, 9 Dec 2004 10:02:30 +0900 Florian Gross wrote: > email55555 email55555 wrote: > > > I am learning Tk from the book "Practical Programming in Tcl and Tk". > > I try to translate the book's examples from Tcl to Ruby. (as exercice > > for myself) > > > > Here is one of kind example: (TCL) > > > > foreach name {one two three four five} { > > label .$name -text $name -bg white > > pack .$name -side top > > } > > pack .five -before .one > > > > [...] > > So, what's your suggestion? Do not using array ? write them one by one ? > > or ... > > > > variables = %w[one two three four five].map {|e| > > TkLabel.new(:text=>name, :bg=>'white') {pack(:side=>:top)}} > > Tk.pack variable[4] :before=>variables[0] > > Yes, I'd use an Array like in your sample. Tk is a bit more chaotic in > this matter which is probably why they are using variables instead. > > You could also use a Hash like this: > > labels = Hash.new > %w[one two three four five].each do |name| > label = TkLabel.new(:text => name, :bg => 'white') do > pack(:side=>:top) > end > labels[name] = label > end > > Tk.pack(label["four"], :before => label["one"]) > Or even (Warning: Typed directly into the mail) labels = %w[one two three four five].inject({}) { | result, name | result[name] = TkLabel.new(:text => name, :bg => 'white') do pack(:side => :top) end result } Tk.pack(label["four"], :before => label["one"]) regards, Brian -- Brian Schr�der http://www.brian-schroeder.de/