Increment Variable for Ruby

I’m new to ruby. Is there a way to shorten this code(“increment var#”)?
I’ve created 26 entry for an alphabet.

require 'tk'
dis = { 'padx' =>5, 'pady' =>5}
root = TkRoot.new {title "Alphabet"}
elpar = {'height' => 25, 'width' => 25}
f1 = TkFrame.new{
    relief 'sunken'
    borderwidth 1
    background "black"
    height 800
    width 800
    dis
    pack
    }
f1.place('x' => 0, 'y' => 0)

el00 = TkEntry.new(f1)
el01 = TkEntry.new(f1)
el02 = TkEntry.new(f1)
          to             # Can I use loop here to shorten it?
el26 = TkEntry.new(f1)   


var1 = TkVariable.new
           to            #(1)
var2 = TkVariable.new


el00.textvariable = var1
var1.value = "A"
el00.place(elpar)
el00.place('x' => 0, 'y' => 0)
          to             #
el26.textvariable = var26
var26.value = "Z"
el26.place(elpar)
el26.place('x' => 728, 'y' => 0)

Tk.mainloop

(1) I’ve tried using loop here

  x = 1
  while x < 27
    "var#{x}"=TkVariable.new         # var1 is still undefined local variable if I use a loop.  
    "var" + x.to_s=TkVariable.new    # another variation that i've tried 
     x += 1
  end

Is there a way to shorten it? My codes are just an increment of 1?

Thanks,
Jim


Source: ruby

Leave a Reply