Home arrow Ruby-on-Rails arrow Ruby Conditionals
RUBY-ON-RAILS

Ruby Conditionals


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.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
October 17, 2007
TABLE OF CONTENTS:
  1. · Ruby Conditionals
  2. · IF Statements
  3. · Else Clause
  4. · Unless Statements

print this article
SEARCH DEVARTICLES

Ruby Conditionals
(Page 1 of 4 )

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.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials