I need to make few scripts for tasks I do often manually, but last time
I programmed something was almost 20 years ago in high school, we used
some BASIC dialect. Since no one uses BASIC nowdays, I decided to give a
try to Ruby (I also tried Python, but it's strict indentation is driving
me insane).
I watched Ruby Essentials Video from Lynda.com (twice), and I pretty
much understand almost all of it, but still, I'm having trouble to adapt
how to structure my programs (without using of GOTO)
Here's a simple example written in FreeBASIC (it works)
···
---------------------------------------------------------------------
Dim As Integer strKeyPress
menu_00:
CLS
PRINT "what do you desire:"
PRINT
PRINT "1 - open menu_01"
PRINT "2 - open menu_02"
PRINT "3 - open menu_03"
PRINT "4 - open menu_04"
PRINT "q - Quit"
REM is comment line, same as ' ... code for Q is 81, q = 113
Do
strKeyPress = GetKey
Loop Until strKeyPress <> 0
'49 represents number 1 on a keybord, and so on
if strKeyPress = 49 THEN
goto menu_01
elseif strKeyPress = 50 THEN
goto menu_02
elseif strKeyPress = 51 THEN
goto menu_03
elseif strKeyPress = 52 THEN
goto menu_04
elseif strKeyPress = 81 or strKeyPress = 113 THEN
goto programendshere
else
print
print "Wrong Choice, Try Again!"
sleep 1000
menu_00_endif:
end if
goto menu_00
menu_01:
cls
print
print "menu 01"
sleep 1000
goto menu_00_endif
menu_02:
cls
print
print "menu 02"
sleep 1000
goto menu_00_endif
menu_03:
cls
print
print "menu 03"
sleep 1000
goto menu_00_endif
menu_04:
cls
print
print "menu 04"
sleep 1000
goto menu_00_endif
programendshere:
End 0
------------------------------------------------------------------
So, what would be the best practice to make this work in modern
languages, like Ruby ? Here are some ideas I have, would be nice to know
if it's good thinking or wrong:
1- make all menues part of a same Class, so I can use instance variables
(I will need to do some calculations)
2- define every menu as separate method, so you travel from root
menu_00 (method) --> menu_01 (which is separate method) --> ... etc go
back and forth ... #could this be memory demanding if there is too many
sub-menu branching ???
3- define every menu as separate method, but you DO NOT start new method
from INSIDE of other method. It goes something like this: menu_00
returns value to a loop, that value is name of sub-menu (menu_01,
separate method) ... I'd then have to use -->
method(name_of_returned_value).call()
This looks better than option #2, but my attempts to make it work so far
failed ... not sure if it is even possible to achieve at all
4- every menu is separate method, but they only have puts "enter choice"
lines, no other code, and a $global variable which I will use later in
IF loops to detect inside which menu (method) I am:
if $global == "00" and userInputChoice == "1"
#if you are currently inside root menu 00, and user input is "1"
menu_01()
$global = "01"
... etc
I think this could easily work, though, might not look nice
.................................................................
So basically, I'd like to know which way should I go for, 2,3 or 4, or
there is maybe even something else? And, also is idea #1 ok ?
I hope this wasn't too long to read for my first question here, starting
something new is always hardest part for me
--
Posted via http://www.ruby-forum.com/.