Ruby/Control structures2

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application
Control structures

Unless is another type of control structure.

The general syntax is:

unless condition
 
       instruction1
 
else
 
instruction2
 
end

The same rule applies, you can have as many instructions executed as you want, I just avoided writing more.

The output:

The program verifies if the condition is true and if it is, it executes instruction1.If the conditions is false, it executes instruction2

If you need something even more powerful in terms of options, you can use the case statement, similar to "switch" in other programming languages.

The general syntax of the case statement:

case variable
 
     when condition1
     intruction1
 
     when condition2
     instruction2
     ...............
 
     when conditionk
     instructionk
 
else instructionk+1
 
end

The outcome:

The program evaluates the value of the "variable".If the value equals condition1, then instruction1 is executed and so on. If none of the condition1 to conditionk are equal to the variable value, then the program will execute instructionk+1

You can also evaluate something with the ternary operator "?" like this:

x = condition ? iftrue : iffalse

The outcome:

x will be assigned the value of "iftrue" if the "condition" is true, and the value of "iffalse" if the condition is false.

An example:

x = y != 5 ? y : 0

If y is different than 5, then x takes the value of y.If y equals 5, x becomes 0.

Previous Next
Personal tools
Interesting Sites
ListSergeant