Home arrow Ruby-on-Rails arrow Page 2 - Ruby Conditionals

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
  1. · Ruby Conditionals
  2. · IF Statements
  3. · Else Clause
  4. · Unless Statements

print this article

Ruby Conditionals - IF Statements
(Page 2 of 4 )

As a nerdy kid, my friend and I would make Qbasic programs whose sole purpose was to insult people. This was back before people even knew what a computer was (I'm talking an old Apple IIe and no user interface). We would program little IF Statements that would ask your name, and if you entered a certain name, it would tell you you had some serious weight issues, or that your mama was so dumb, she threw a rock at the ground and missed it. Insult gold is what I'm telling you. And all thanks to the good old If Statement.

Try the following code:

hammer_time = 2

if hammer_time > 1 && hammer_time < 3 # begins IF conditional

puts “Hammer Time!” # tells it what to do if the condition is met

end # ends the conditional

If you run this program, you will clearly see that it is indeed, Hammer Time.

In the above code, we are saying that if the variable hammer_time is greater than 1 AND less than three, print the words, “Hammer Time!” to the screen. We give the clause AND by using the && operator.

We could also use the OR (||) operator to specify that we want the value to equal this OR that. Like so:

hammer_time = 2

if hammer_time >1 || hammer_time < 3

puts “Hammer Time!”


Again, since hammer_time meets the criteria, “Hammer Time!” is written to the screen.

We could have made it even simpler had we wanted:

hammer_time = 2

if hammer_time > 1

puts “Hammer Time!”


All of the above code is an example of a Boolean condition. Boolean refers to a true/false situation (or Yes/No or On/Off). In other words, it's not like those multiple choice questions you got on your test.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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