Home arrow Ruby-on-Rails arrow Page 4 - 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: 4 stars4 stars4 stars4 stars4 stars / 3
October 17, 2007
TABLE OF CONTENTS:
  1. · Ruby Conditionals
  2. · IF Statements
  3. · Else Clause
  4. · Unless Statements

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Ruby Conditionals - Unless Statements
(Page 4 of 4 )

With IF statements, you are really seeking a True result from your Boolean question. With Unless statements, you are seeking just the opposite -- the False Boolean response.

Note that you cannot use elseif clauses in Unless statements.


hammer_time = 2


unless hammer_time == 2

puts “Ice Ice Baby!”

else

puts “Hammer Time!”

end

The above would result in: “Hammer Time!”

Case Statements

If you have a lot of If and elseif clauses, one way you can save time is by using the case statement. Here is an example: 


action_type = “Flex”


case action

when “Walk”

puts “You are walking.”

when “Jog”

puts “You are jogging.”

when “Run”

puts “You are running”

when “Eat”

puts “Quit eating you fat pig!”

when “Flex”

puts “I didn't know muscles jiggled.”

else

puts “What? Sorry my hearing aid fell out, say it again.”

end

You could write If and else clauses for all of the above, but as you'll see it would constitute a lot more code. And let's face it, you're a lazy schlub.

 

action_type = “Flex”


if action_type == “Flex”

puts “I didn't know muscles jiggled.”

elseif action_type == “Walk”

puts “You are walking.”

elseif action_type == “Jog”

puts “You are Jogging.”

elseif action_type == “Run”

puts “You are running.”

else action_type == “Eat”

puts “Quit eating you fat pig!”

else

puts “What? Sorry my hearing aid fell out, say it again.”

end

As you can see, this can be time consuming. You can also use ranges within case statements. It looks like this:


yearly_salary = 40,000

case yearly_salary

when 0...10,000

puts “That's my monthly food bill.”

when 100,000...1,000,000

puts “I'm RIOTCH SNIOTCH!”

else

puts “Yeah, somewhere in that range.”

end

Well, we covered a lot of ground in that tutorial, but we still have more to cover. Be sure to come back next week, when I'll cover how to use Loops, how to call Methods, and maybe even how to not be so nerdy. Until then...


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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