Ruby Conditionals
(Page 1 of 4 )
In the last article I left you with a cliffhanger; if you are here, then it worked. We previously discussed ranges and variables, and now we are going to speak about conditionals. But first, let's touch on ranges a bit more.
M&M's
I don't know about you, but I've never had an M&M melt in my mouth. Maybe that's because I shovel them in my mouth and swallow them before they have a chance. But never mind that; for this article we are talking about a different kind of M&M.
Minimum and maximum ranges in Ruby allow you to retrieve the highest and lowest ranking of a set of data.
Range = 1..10
puts range.min # prints the minimum value in the range
puts range.max # prints the maximum value in the range
The above code would print the following to the screen:
1
10
Conditionals
You've used conditionals all your life. Remember back in high school when you were trying to get Betty Sue to be your date and you said, “I'll pay you fifty bucks!” and she said she would think about it? Well, that was a simple conditional. You were saying if I do this, you do that. Now what if she had agreed and you gave her the money and she showed up with Biff Robertson instead of you? Well, if you were smart, you would have planned on just that sort of thing happening. Here is how that plan might have looked in pseudo code:
If Betty Sue accepts My fifty dollars
I have a date
Unless Betty Sue shows up with Biff Robertson
Then I “accidentally” spill my spiked kool-aid on her dress
In code terms, the above would be an If statement with an else clause, meaning do this if this occurs, else do something else. There are several types of Conditionals in Ruby. We'll start with the IF statement.
Next: IF Statements >>
More Ruby-on-Rails Articles
More By James Payne