[#403837] Why none of the block giving the expected output with the "enumerator"? — Arup Rakshit <lists@...>

Why none of the block giving the expected output with the "enumerator"?

9 messages 2013/02/02

[#403870] Confusion with Enum#with_object block argument construct — Arup Rakshit <lists@...>

C:\>irb

9 messages 2013/02/03

[#403920] Character classes use in Ruby — Love U Ruby <lists@...>

Can anyone help me by giving an explanatory example of each of the

13 messages 2013/02/04

[#403935] How to stop page loading using selenium-web driver? — Love U Ruby <lists@...>

How to stop page loading using selenium-web driver?

11 messages 2013/02/04

[#403972] Ruby could recognize the values when putting into a webpage text filed. — Love U Ruby <lists@...>

Hi,

9 messages 2013/02/05

[#403986] old syntax? what's going on here — tamouse mailing lists <tamouse.lists@...>

I have this in a _spec.rb file: (a gem I inherited at work)

11 messages 2013/02/05

[#404005] Implementing DRY with a function call — Rob Marshall <lists@...>

Hi,

12 messages 2013/02/05

[#404006] using an instance variable inside a method — FirstName Surname <lists@...>

Hello.

19 messages 2013/02/05

[#404021] Not able get the label text incurred with <input> element — Love U Ruby <lists@...>

I do have a below `HTML`:

18 messages 2013/02/05

[#404025] Symbol.defined? — Student Jr <lists@...>

Symbol deserialization from external sources is now known to be

15 messages 2013/02/06

[#404058] Ruby 1.9.3-p362 on Mac OSX — Peter Bailey <lists@...>

Hi,

13 messages 2013/02/06

[#404082] Problem building Ruby 1.9.3 patchlevel 385 under AIX 7.1 — Ruby Student <ruby.student@...>

Hello World!

10 messages 2013/02/06

[#404101] Issues from an extreme beginner — Bruce Palmer <lists@...>

Hey guys, great to be part of such a great community! I look forward to

19 messages 2013/02/07
[#404104] Re: Issues from an extreme beginner — Bruce Palmer <lists@...> 2013/02/07

Ahh, thank you Matthew! That was just the push I needed!

[#404208] elegant way to determine if something is defined — tamouse mailing lists <tamouse.lists@...>

Something like:

15 messages 2013/02/10

[#404218] Ruby Equivalent to VB's "With"? — Joel Pearson <lists@...>

I've looked around but I couldn't find anything helpful on this,

11 messages 2013/02/10

[#404235] The "ruby way" to do desktop applications? — "guirec c." <lists@...>

Hello,

17 messages 2013/02/11

[#404238] Best books for "advanced" programmers — "guirec c." <lists@...>

Hello,

18 messages 2013/02/11

[#404245] Issue with Excel column values read. — Love U Ruby <lists@...>

Hi,

22 messages 2013/02/11

[#404344] Ruby command line options s and S — Love U Ruby <lists@...>

Can anyone help me to understand the difference between s and S with

15 messages 2013/02/13

[#404386] Re: Ruby command line options s and S — "D. Deryl Downey" <me@...>

Dude!

20 messages 2013/02/14
[#404397] Re: Ruby command line options s and S — Love U Ruby <lists@...> 2013/02/14

Humm!

[#404387] Ruby Multithreaded producer-consumer problem — Abhijit Sarkar <lists@...>

Hi,

26 messages 2013/02/14
[#404896] Re: Ruby Multithreaded producer-consumer problem — Abhijit Sarkar <lists@...> 2013/02/24

Bump!

[#404456] skip iteration in each loop — Saurav Chakraborty <lists@...>

I want to skip iteration for few values depending on dynamic condition.

11 messages 2013/02/15

[#404491] so, what's the proper way to replace funcionality of GOTO ? — "Stu P. D'naim" <lists@...>

I need to make few scripts for tasks I do often manually, but last time

27 messages 2013/02/15
[#404492] Re: [from BASIC to Ruby] so, what's the proper way to replace funcionality of GOTO ? — Love U Ruby <lists@...> 2013/02/15

Stu P. D'naim wrote in post #1097111:

[#404494] Re: [from BASIC to Ruby] so, what's the proper way to replace funcionality of GOTO ? — Ryan Victory <ryan@...> 2013/02/15

Love U Ruby: I'm really not sure what you meant by that response, but

[#404570] What is Ruby's default constructor? — Love U Ruby <lists@...>

Hi,

12 messages 2013/02/17

[#404632] Re: splat operator and Ruby instance variable assignments — Marc Heiler <lists@...>

Ok, understood what the guy wants ...

17 messages 2013/02/19
[#404640] Re: splat operator and Ruby instance variable assignments — Love U Ruby <lists@...> 2013/02/19

Marc Heiler wrote in post #1097736:

[#404645] Re: splat operator and Ruby instance variable assignments — Ryan Davis <ryand-ruby@...> 2013/02/19

[#404646] Re: splat operator and Ruby instance variable assignments — Love U Ruby <lists@...> 2013/02/19

Ryan Davis wrote in post #1097840:

[#404647] Re: splat operator and Ruby instance variable assignments — Peter Hickman <peterhickman386@...> 2013/02/19

On 19 February 2013 20:35, Love U Ruby <lists@ruby-forum.com> wrote:

[#404648] Re: splat operator and Ruby instance variable assignments — Love U Ruby <lists@...> 2013/02/19

Peter Hickman wrote in post #1097848:

[#404696] THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — "Xavier R." <lists@...>

Hi,

25 messages 2013/02/20
[#404699] Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — Matt Mongeau <halogenandtoast@...> 2013/02/20

Maybe you could provide more detail about what you are confused about. To

[#404700] Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — "Xavier R." <lists@...> 2013/02/20

Matt Mongeau wrote in post #1098058:

[#404705] Re: THE CLASS/OBJECT CHICKEN-AND-EGG PARADOX — Matt Mongeau <halogenandtoast@...> 2013/02/20

It's not really a paradox. Take for example

[#404738] backslash substitution — Mario Ruiz <lists@...>

don't know why... but this is not working

18 messages 2013/02/21

[#404809] Difference of 2 dates interms of years. — "Xavier R." <lists@...>

how can we get the experience years between two dates(ex:2012-01-11 to

13 messages 2013/02/22

[#404817] Not able to understand the difference between "||=" and "|=". — "Xavier R." <lists@...>

>> a = []

12 messages 2013/02/22

[#404839] range is not assigning to the splat variable. — Love U Ruby <lists@...>

Why splat variable couldn't take in the below two code the "range" ->

10 messages 2013/02/23

[#404842] Why class returning its own name when "include" statement? — Love U Ruby <lists@...>

I was actually playing around with the class definition return values.

16 messages 2013/02/23
[#404844] Re: Why class returning its own name when "include" statement? — Love U Ruby <lists@...> 2013/02/23

@Stefano Yes you are right. The below code is proved that.

[#404867] how to see the class creation time in Ruby? — Love U Ruby <lists@...>

In Ruby any chance to see the last-modified time of a specific class?

15 messages 2013/02/23

[#404901] Confusion with `nil` value being produced by IRB in case of Array#size manipulation. — Love U Ruby <lists@...>

enum[int] = obj → obj

10 messages 2013/02/24

[#404921] How should I print only the last combination when using Array#combination(n) ? — Love U Ruby <lists@...>

>> a = [1,2,3]

15 messages 2013/02/24

[#405026] Please, help (GCD) greatest common divisor. — Caddy Tonks Lupin <lists@...>

Write a program to read two integers and show their greatest common

17 messages 2013/02/26

[#405059] Does this specific sound library exist? — Dirk Vogel <lists@...>

Hi there,

16 messages 2013/02/26

[#405067] Mac OS 10.8.2 and openssl — "Dr. Hegewald" <hegewald@...>

Hi everybody,

24 messages 2013/02/27

[#405079] Why `10` not returned without the `return` from the block ? — Love U Ruby <lists@...>

CODE - I

10 messages 2013/02/27

[#405107] Object track llist for a particular class. — "Xavier R." <lists@...>

Say I have created more than one instances from a particular class as

13 messages 2013/02/27

[#405145] Discussion on Ruby's `alias` — Tukai Patra <lists@...>

>> class Foo

27 messages 2013/02/28

[#405175] telnet - how to loop through commands listed in a file — Bob Ford <lists@...>

Let me first explain what I'm trying to do. I have written a very

18 messages 2013/02/28

Re: [from BASIC to Ruby] so, what's the proper way to replace funcionality of GOTO ?

From: "WILLS, JAMAL A" <jw1496@...>
Date: 2013-02-15 17:39:05 UTC
List: ruby-talk #404510
There is no one way to replace GOTO.  GOTO is at the heart of many structured control structures.  What you need to do is think about what you are trying to do and where you are trying to go when you are done.

In your code, you have: "goto menu_01".  After the sequence of statements starting at the menu_01 label, you have:  "goto menu_00_endif".  If you returned from your sequence to the goto, you would have a subroutine.  I'm not familiar with FreeBASIC, but in Commodore BASIC, you could replace your GOTO with GOSUB and the GOTO at the end of the sequence with RETURN.  That would eliminate the need for the "menu_00_endif" label.  This is a subroutine and can be replaced by a method in Ruby.  I think in FreeBASIC, your sub may be defined as follows:

' I think "goto menu_01" may be replaced by
Menu_01()

' and the subroutine by
Sub Menu_01
   ...
End Sub

You also have a "goto menu_00" after the "End If" statement.  The menu_00 and its goto can be replaced with a loop.

Using a while loop would be a good idea.  The loop using goto menu_00 is an infinite loop, but you use "goto programendshere" to exit it.  By setting a condition variable for the loop, you can exit the loop by changing the variable.  I'm taking a guess at the FreeBASIC grammar here:

    stillChoosing = True
    Do While stillChoosing
      ...
      ElseIf strKeyPress = 81 Or strKeyPress = 113 Then
        stillChoosing = False
      Else
     ...
    Loop

It looks like your code is somewhat structured already.  You just need to learn how control structures are built from gotos and other statements to properly replace them.

Finally, since all you your menus will have the same structure, it make sense that you can replace multiple subroutines with just one.  You can pass the text of each menu and return the result.  (In Basic, that will make it a Function rather than a Sub, I believe!  But either way, it would be a Method in Ruby.)

Putting all that together in Ruby might look something like this:

# Displays a menu and gets a response back.
def showMenu menuText
  # Display the menu
  puts menuText
  puts
  # Display a prompt
  print "What do you desire? "

  # Get a string as a response.  (The user will have to press enter, unlike your example.)
  # chomp removes a newline from the end of the string
  # downcase makes the string lowercase so "Q" and "q" are returned as "q"
  gets.chomp.downcase
end

# Here I am putting the menu text in a long string.
menu_00 = %q{menu 00

1 - open menu_01
2 - open menu_02
3 - open menu_03
4 - open menu_04
q  - Quit}

# The same can be done for these when they are fleshed out
menu_01 = "menu 01"
menu_02 = "menu 02"
menu_03 = "menu 03"
menu_04 = "menu 04"

stillChoosing = true
while stillChoosing do
  case showMenu(menu_00)
    when "1"
      response = showMenu(menu_01)
      # do something with response here
    when "2"
      response = showMenu(menu_02)
      # do something with response here
    when "3"
      response = showMenu(menu_03)
      # do something with response here
    when "4"
      response = showMenu(menu_04)
      # do something with response here
    when "q"
      stillChoosing = false
    else
      puts "Wrong Choice, Try Again!"
      puts
  end
end

-----Original Message-----
From: Stu P. D'naim [mailto:lists@ruby-forum.com] 
Sent: Friday, February 15, 2013 11:32 AM
To: ruby-talk ML
Subject: [from BASIC to Ruby] so, what's the proper way to replace funcionality of GOTO ?

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/.

In This Thread