I have been learning Ruby for three months. I am making a GUI (using tk) with a Spanish automatic verb conjugator so that when you type a verb form you get a morphological analysis of it and then the complete conjugation table. A Spanish verb can support multiple forms, so I had the idea to use Comboboxes for selecting which tense the user wants to see on screen.
When the typed form is unique, there is no problem. But, what if the same form can be used with two verbs -or three or even more?
Well, in that case I used a while cycle. On the analysis screen you get first the morphological analysis of the first occurrence, the corresponding comboboxes, and then the second analysis, and so on. The static variables can have the same name, because the user just does not interact with them.
Now the problem seems to be in the comboboxes themselves. When I bind ComboboxSelected to any of the comboboxes shown on screen, the program shows the conjugation of the last occurrence. So what I have done is to create dynamic variable names using instance_variable_set. So if it is the first occurrence, the first combobox will be called combo1, the frame in which it is inserted is frame1, and so on.
Then I solved the problem but only virtually. I created different names for the comboboxes and the tables, but when I try to bind any combobox with their corresponding tables, I realize that I must specify which combobox I select, so I have to write code for every combobox binding instead of writing code once for any combobox. If a verb form has 10 occurrences, I have to write 10 different codes with almost the same algorithm (only changing the number), which I think is crazy.
My question is: is there a way to write code so you can USE (not create) a dynamic variable without specifying which instance is? Note that I just don’t want that the different comboboxes display the same conjugation. I want that the combobox1 displays the conjugation of the first occurrence, the combobox2 displays the conjugation of the second… and so on. I want to do this without writing the algorithm one by one. Is this possible?
I’ll show you some code (this is inside a WHILE cycle -variable conj represents the number of occurrences a verbal form has)
instance_variable_set("@comboboxtema_#{conj}", Tk::Tile::Combobox.new(framemedio) { font TkFont.new('calibri 14'); pack('pady'=>5); width 47; \
set "Seleccioná el Tema Verbal cuya conjugación querés visualizar"; state 'readonly'; values temasdisponibles})
instance_variable_set("@framevoz_#{conj}", TkFrame.new(framemedio) { height(0); width(0); pack('pady'=>0) })
instance_variable_set("@comboboxvoz_#{conj}", Tk::Tile::Combobox.new)
instance_variable_set("@comboboxmodo_#{conj}", Tk::Tile::Combobox.new)
instance_variable_set("@tabla_#{conj}", TkFrame.new)
The problem is below. Is there a way to make it more automatic so I don’t have to replace comboboxtema_3 for comboboxtema_2, and so on? Note that I am supposing there will only until three different occurrences for the same form… There could be more but my code will not care about them.
@comboboxtema_3.bind("<ComboboxSelected>") do
@comboboxvoz_3.destroy
@comboboxmodo_3.destroy
@tabla_3.destroy
@comboboxvoz_3 = Tk::Tile::Combobox.new(@framevoz_3) {font TkFont.new('calibri 14'); pack('pady'=>5); width 40; \
set "Seleccioná la Voz cuya conjugación querés visualizar"; state 'readonly' }
@comboboxvoz_3.values = ["Voz Activa", "Voz Pasiva"]
@comboboxvoz_3.bind("<ComboboxSelected>") do
@comboboxmodo_3.destroy
@tabla_3.destroy
@comboboxmodo_3 = Tk::Tile::Combobox.new(@framevoz_3) {font TkFont.new('calibri 14'); pack('pady'=>5); width 45; \
set "Seleccioná el Modo cuya conjugación querés visualizar"; state 'readonly' }
@comboboxmodo_3.values = ["Indicativo", "Subjuntivo", "Imperativo"]
@comboboxmodo_3.bind("<ComboboxSelected>") do
@tabla_3.destroy
@tabla_3 = TkFrame.new(@framevoz_3) { height(0); width(0); pack('pady'=>0) }
celver10 = TkLabel.new(@tabla_3) {text "Indicativo Presente "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>0, 'column'=>1, 'padx'=>1, 'pady'=>1)}
celver10 = TkLabel.new(@tabla_3) {text "Indicativo Pasado "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>0, 'column'=>2, 'padx'=>1, 'pady'=>1)}
celver10 = TkLabel.new(@tabla_3) {text "1ra Persona Singular "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>1, 'column'=>0, 'padx'=>1, 'pady'=>1)}
celver20 = TkLabel.new(@tabla_3) {text "2da Persona Singular "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>2, 'column'=>0, 'padx'=>1, 'pady'=>1)}
celver30 = TkLabel.new(@tabla_3) {text "3ra Persona Singular "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>3, 'column'=>0, 'padx'=>1, 'pady'=>1)}
celver10 = TkLabel.new(@tabla_3) {text "1ra Persona Plural "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>4, 'column'=>0, 'padx'=>1, 'pady'=>1)}
celver20 = TkLabel.new(@tabla_3) {text "2da Persona Plural "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>5, 'column'=>0, 'padx'=>1, 'pady'=>1)}
celver30 = TkLabel.new(@tabla_3) {text "3ra Persona Plural "; font TkFont.new('calibri 14 bold italic'); anchor "w"; justify "left"; relief "groove"; grid('sticky'=>'nsew', 'row'=>6, 'column'=>0, 'padx'=>1, 'pady'=>1)}
end
end
end