One of the most integral parts to programming is the basic IF statement or the conditional Statement. Ruby like most if not all languages uses the if keyword to define an IF statement. However it defers from other languages as you can use the if keyword as a statement or a modifier. In addition Ruby has the unless keyword to perform the else if logic without testing for a negative result.

The basic IF ELSE statement

The IF statement in Ruby is the same as Java or C++ with the following differences like methods you don’t need to utilize parenthesis and ELSE IF has been shortened into the elsif keyword. Lets start by looking at a basic example:

if var < 10 
    puts "The variable is less than ten."
elsif var >= 10 and var < 25
    puts "The variable is less than 25 but greater or equal to ten."
else
    puts "The variable is greater then or equal to 25."
end

The IF modifier

In addition to defining IF statements ruby lets you use conditionals as a modifier. For example you can write your command and append the if modifier so that the command only executes if the test conditional is true.

puts "The variable is less than 0" IF var < 0

In the example we have written our code for outputting our string but modified it by saying only output it IF our condition is met.

The UNLESS keyword

Ruby in addition to implementing the basic IF ELSE conditional has added in the unless keyword to their language. This keyword is used to say execute the following code if and only if this condition is not met.

if var < 0
    puts "The variable is greater than 0"
else
    puts "The variable is less than 0"
end

The unless keyword is really only a convenience method for those cases your are testing for false, so instead of negating the conditional statement you can simply use unless. Like the if keyword it can also be used as a modifier.

puts "The variable is less than 0" unless var >= 0

Not sure why it was determined that the unless keyword exists in Ruby as everything it can be used for can be completed with an if statement. However from what I have seen Ruby tries to let developers do whatever they want without imposing any restrictions so i guess writing unless is easier then a negated conditional for some.

, , , , , , ,

I am always curious in learning new programming languages and have been curious for awhile now with Ruby. I have come across various scripts and applications built upon it but have never had the professional need to utilize it. Time I guess to bite the bullet and see what if anything is special about this language built on the old but probably ever living C language.

Installing Ruby

The first thing to do, with any language is installation. What to do I guess first off since I use a Windows machine is to see if thee are any executable Ruby installers. Bingo, hello RubyInstaller a self contained ruby language installer with an inclusive execution environment built in.

Creating my first Ruby script

The common file extension to a ruby script file is .rb. So starting things out I created a test script to echo the basic “Hello World” script to verify Ruby was installed correctly.

puts "hello World!"

A couple things right off the back Ruby does not use semi-colons (;) to terminate a code statement, instead it uses the carriage return. Secondly the command to output to the console is puts… A strange word to use for outputting to the system it is simpler then the Java equivalent of System.out to be sure but the PHP use of echo seems more intuitive then puts.

Being familiar with the command prompt I decided to fire it up and run my script using the new installed ruby library. Having selected to add the library to my environments path from the installer there is no need to specify the path to the Ruby library.

ruby test.rb

I was pleasently delighted to have my script output correctly.

C:\test>ruby test.rb
Hello World!

Splitting a ruby command onto multiple lines

Now knowing that Ruby utilizes the new line character to specify the end of a command, I was curious as to how to wraps my ruby commands onto multiple lines. As an avid developer I try to follow common guidelines when writing code one of which is keeping each line of code to within 80 characters improving readability. A quick search revealed that the front slash (\) character can be used to “join” two code lines together.
For example:

test = 10 + 16 + \
9 + 3

will output:

38

Lots more to learn and explore next I’m going to tackle Classes and Methods in Ruby with the hope of gathering some insight into why some developers prefer Ruby as their language of choice.

, , , , , ,