Archive for April, 2008
Unless
Unless is another control structure similar to “if..else’ that is for conditional processing and it is used as follows:
unless condition
instruction1
else
instruction2
end
Again, it uses the “end” statement to terminate the control structure. The “case”, control is more of a screening process that allows say the element of an array or hash to be evaluated to meet a specific set of conditions. It is used as follows:
case variable
when condition1
instruction1
when condition2
instruction2
……
……
when conditionX
instructionX
else instructionA
end
Sample “if…elseif….else”
if condition1 (is true)
instruction a
instruction b
.
.
instructionX
elseif condition2 (if the condtition 1 returns false evaluate with condition2)
instruction c
instruction d
.
.
instructionX
else (if the result is false for both levels then the following code is executed)
instruction e
instruction f
.
.
instructionX
end
The control structure evaluates the necessary ‘contition1′, if it returns true if executes instructions that follow the if statement. If it is false, it proceeds to the elseif using ‘condition2′ to evaluate it, if it satisfies condition2, then it executes code that immediately follows the elseif control. If both evaluations return false, it then proceeds to execute the instructions after the else control.
No commentsControl structures
Control structures are blocks of code that result in either a true or false and are more commonly called loops. Loops are dependent on the satisfaction of certain conditions like in the sample program below that uses the “if..else control(loop):
if condition1 (if condition 1 is true)
instruction 1
instruction 2
.
.
instructionX
else (if condition 1 is false)
instruction 3
instruction 4
.
.
instructionX
end
As you can see, the simplicity of Ruby has it working without the need for the curly braces as with C++. Ruby simply uses the “end” keyword to signify the end of the “if..else” control structure. Another way of doing the same thing but with more conditional procesing is with the the “if..elseif..else” control structure. Syntax is shown in the following post.
No commentsFunctions and Built-in Functions
A function is a way of shortening repetitive parts or blocks of code that are called again and again. Their use benefits the user by first of course, shortening the amount of code lines for having the same block of code appearing again and again wastes time and effort. It also allows the same block of code to be accessed by more than one program. It allows the programmer to break up complex tasks into smaller applications that are easily understood, and last, it allows the programmer to create and hide critical functions such as password and username encryption and decryption routines. Functions are mostly user defined but there are a couple of dozen or so that have been pre-defined and are included in the standard Ruby distribution package. Being open sourced, there are a whole range of functions that have been created and shared by the many ruby programmers out there that is shared across the whole globe.
No commentsRanges
Ranges can be better defined as a numeric array. Ranges can be defined as such:
numrange = 1..20 (numrange holds all the numbers between 1 and 20, effectively turning it into an array)
numrange2 = 1…20 (is another way of defining ranges, holding all numbers between 1 and 20 except the last number 20)
As with previous arrays and hashes, several operations can be done on them like:
puts numrange.min (gives you the lowest number in the specified range)
puts numrange.maxn (gives you the highest number in the specified range)
To check if a specified number is part of the range
numrange.include? (14)
To show the contents of the specified range:
puts numrange.to_a
No comments




