Ruby If, Else If Command Syntax
The Ruby language has a very simple control structure that is easy to read and follow.
If syntax
Here’s the key difference between Ruby and most other languages. Note that “else if” is actually spelled “elsif” without the e.
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”.
If syntax
if var == 10If Else Syntax
print “Variable is 10”
end
if var == 10If Else If Syntax
print “Variable is 10”
else
print “Variable is something else”
end
Here’s the key difference between Ruby and most other languages. Note that “else if” is actually spelled “elsif” without the e.
if var == 10Ternary (shortened if statement) Syntax
print “Variable is 10”
elsif var == “20”
print “Variable is 20”
else
print “Variable is something else”
end
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”)
Ruby If, Else If Command Syntax
Reviewed by Pakainfo
on
August 08, 2018
Rating:
No comments: