Subscribe to How-To Geek

Ruby If, Else If Command Syntax

The Ruby language has a very simple control structure that is easy to read and follow.

If syntax

if var == 10
     print “Variable is 10″
end

If Else Syntax

if var == 10
   print “Variable is 10″
else
   print “Variable is something else”
end

If Else If Syntax

Here’s the key difference between Ruby and most other languages. Note that “else if” is actually spelled “elsif” without the e.

if var == 10
   print “Variable is 10″
elsif var == “20″
   print “Variable is 20″
else
   print “Variable is something else”
end

Ternary (shortened if statement) Syntax

Ternary syntax is the same in Ruby as most languages. The following sample will print “The variable is 10″ if var is equal to 10. Otherwise it will print “The variable is Not 10″.

print “The variable is ” + (var == 10 ? “10″ : “Not 10″)

| More
This article was originally written on 11/20/09 Tagged with: Ruby

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
mDNSResponder.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.