Author Topic: Ruby Junctions  (Read 5446 times)

0 Members and 3 Guests are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Ruby Junctions
« Reply #15 on: July 24, 2011, 10:41:45 am »
Woah nice Ashbad! This reminds me when Tari (or was it Tanner? I don't really remember) made a Switch class for Python on Cemetech :)

Glad you like it :) yeah, I think tanner did the first one, and Kllrnohj came back the next day with an even more kick-ass one.  Fortunately Ruby already has built-in capabilities for switches :P

I don't see the need for switches, but yeah it'd be good to have them default.

Oh, switches are quite useful.  I used them multiple times when writing the current version of this library ;)

They are useful, but they are easily replaced by if's. Give me an example when I can't use an if, and switch is only option?

Switches are good for code organization and easier writing though :)

Ashbad

  • Guest
Re: Ruby Junctions
« Reply #16 on: July 24, 2011, 10:46:02 am »
Yeah, switches are totally replaceable by if statements, and they are more limited since they must evaluate an expression first and the different branches are determined by the outcome of the result expression.  However, in cases where you're doing things like "x==1", "elsif x==2", etc. Switches are slightly more optimized and easier to read.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Ruby Junctions
« Reply #17 on: July 24, 2011, 10:48:12 am »
Yeah, switches are totally replaceable by if statements, and they are more limited since they must evaluate an expression first and the different branches are determined by the outcome of the result expression.  However, in cases where you're doing things like "x==1", "elsif x==2", etc. Switches are slightly more optimized and easier to read.

As an example:

Check if a var equals 1, 2 or 3 and if none print "Error".

Code: [Select]
if var==1
  print "It's 1"
else-if var==2
  print "It's 2"
else-if var==3
  print "It's 3"
else:
  print "Error"

Code: [Select]
switch var;
case 1:
  print "It's 1"
case 2:
  print "It's 2"
case 3:
  print "It's 3"
default:
  print "Error"

It's +- the same thing.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Ruby Junctions
« Reply #18 on: July 24, 2011, 08:21:14 pm »
I like switches better, though.  I was able to use them in C++ and found them to be really easy to use. :)