-1

How can I do something like this in Ruby?

if variable = something  
    do A  
    do B  
    do D  
elsif variable = something different
    do A
    do B
    do C
    do D
else
    do A
    do C
    do D

A = set of loops with if else
B = set of loops with if else
C = set of loops with if else
D = final steps

Looking for a way to accomplish something like this in Ruby. I'm sure this question is answered somewhere but I don't know what it would be called. I found some information about a gem that allows you to use goto but I would prefer to learn the correct way to do this (also it seems that this might be a joke). I'd prefer to write the code out myself but I can put my actual code up if that helps answer my question.

If someone could just point me in the right direction.

Also if goto isn't a joke why is it not okay to use?

tmfahall
  • 172
  • 1
  • 13
  • 5
    Define functions `A(), B(), C(), D()` and call them as appropriate. Pass `variable` as a parameter if needed. – Michael Berkowski Apr 21 '14 at 17:56
  • 1
    http://en.wikibooks.org/wiki/Ruby_Programming/Writing_methods – Michael Berkowski Apr 21 '14 at 17:58
  • 1
    Almost certainly the design of your application is 'wrong'. Even if it spits outs the correct answer, ruby was not designed to have large methods with large amounts of control flow. It supports many ways to create methods and carefully define when they are called (modules, classes, inheritance, includes, lambdas/blocks), and it does not have a goto. http://patshaughnessy.net/2012/2/29/the-joke-is-on-us-how-ruby-1-9-supports-the-goto-statement – Sqeaky Apr 21 '14 at 20:16
  • 2
    `A` and `D` are always performed here; get them out of the decision blocks. – jscs Apr 25 '14 at 20:35
  • Why not *just* compile your own version of ruby with support for `goto` statements? http://patshaughnessy.net/2012/2/29/the-joke-is-on-us-how-ruby-1-9-supports-the-goto-statement … FYI: this is not a sensible thing to do, but is amusing that it's even possible – maniacalrobot Apr 22 '16 at 10:07
  • @maniacalrobot I'd hate to ever inherit some Ruby where someone decided that was the way to go. I cringe looking back on some of my early questions. This was clearly me not understanding what I was doing at all. – tmfahall Apr 22 '16 at 16:39

3 Answers3

7

Instead of goto just create functions for A, B and so on and use them

for example:

def A
  # Lot of code
end

Now you can goto A by just writing A.

Also instead of using if/else you can use switch case, so that your code will look like

A
case variable
when something
  B
  D
when something else
  B
  C
  D
else
  C
  D
end
Saurabh
  • 71,488
  • 40
  • 181
  • 244
  • @noMAD. I think that using functions will suit me best. But using switch case is something that I will look into. Thank you for all your help. – tmfahall Apr 21 '14 at 18:09
  • @tmfahall I see you went with the switch case. Good, its the right way. – noMAD Apr 27 '15 at 16:00
0

This is a classic use for switch case. Here is how you do it in ruby. Also, I would make all the do's methods and call them from inside when

Community
  • 1
  • 1
noMAD
  • 7,744
  • 19
  • 56
  • 94
0

I have created a working label + goto gist, it's pretty simple but it's my stab at it, if you feel like it, take a look at this gist

Code usage example:

label_stack_main do
  x = 0

  label(:increment) do
    x += 1
    goto :example
  end

  label(:example) do
    puts x
    goto :increment if x < 10
  end
  
  label(:start) do
    goto :example
  end
end

Outputs the following:

user@machine:~/path/to/projects/labels_test/$ ruby labels_test.rb 
0
1
2
3
4
5
6
7
8
9
10

Hope somebody appreciates this :)

Shannarra
  • 521
  • 4
  • 17