dreamsys software
|
Like Progressive Metal? Check out my band on Spotify, please follow us! ![]() |
Ruby Scripting Tutorial
If/Else In order for a script to be very useful, you will need to be able to test the conditions of variables. Most programming and scripting languages have some sort of if/else expression and so does ruby. Let's do a simple script that will ask a user for a password before allowing him to continue. This is obviously not how you would implement such security in a real system, but it will make a good example of using if and else statements. #!/usr/bin/ruby # This is some secure program that uses security. $validPassword = 'secret' #this is our password. puts "Please Enter Password: " $inputPassword = gets.chomp if $inputPassword == $validPassword puts 'You have access!' else abort('Access denied!') end print 'Welcome!' In this example, the "is equal to" comparison is used in the if condition. This is represented by the "==" operator. If you wanted to do the check "is not equal to", you could use the operator "!=". Below is a table of some of the comparisons you can use in your scripts: Comparisons:
Another thing new to you in this script is the abort function, which is used to exit a ruby script. The string parameter to the function will be displayed to the user. The use of gets.chomp is also shown in this example. This will wait for the user to type input and hit enter. The input is returned by the function into the variable "inputPassword". Let's enhance our years.rb script to include some better checking. This time we will prompt for the variables if they are not given on the command line. #!/usr/bin/ruby # Years till 100 if ARGV.length > 1 $name = ARGV[0] else print "Enter Name: " $name = gets.chomp end if ARGV.length > 2 $age = Integer(ARGV[1]) else print "Enter Age: " $age = Integer(gets.chomp) end $sayHello = 'Hello ' + $name + ', ' if $age == 100 $sayAge = 'You are already 100 years old!' elsif $age < 100 $sayAge = 'You will be 100 in ' + String(100 - $age) + ' years!' else $sayAge = 'You turned 100 ' + String($age - 100) + ' years ago!' end puts $sayHello + $sayAge With this script, you do input in several ways. You can give the name and age on the command line, you can give just the name on the command line and read the age from user input, or you can give nothing on the command line and read name and age from user input. When reading from user input to a number (integer) variable, you must use the Integer() cast. Also, when appending to a string with the + operator, you must cast any int value (such as "100 - $age") to a string with the String() cast. This script also introduces a new keyword, elsif this is the "else if" of ruby. Notice that our if conditions now have 3 checks, if the age is 100, if the age is less than 100, and everything else ($age > 100). We could have put elsif $age > 100 in place of the else keyword, but they would do the same thing in this situation. There is still something missing in this script. How is the user to know what the input should be on the command line? What if the user assumes that the age comes before the name? The script will crash. One way that this can be resolved is to use the powerful OptionParser in the optparse package, which we will discuss in the next section. |