Last modified: Thu Aug 16 2018 22:49:02 GMT+0800 (Malay Peninsula Standard Time)

Chapter 1. Ruby Basics

This chapter will provide you some basics of Ruby programming language. All the examples shown in this chapter will be written and ran in the form of Ruby IRB (Interactive Ruby Shell) or Ruby script. The result of both method will be similar or identical depending on how you write it. However, almost all the example in this chapter will be written in the form of script.

1.1 Ruby IRB

To use Ruby IRB (Interactive Ruby Shell), simply use the command irb in your terminal window. Figure 1.1.1 shows the Interactive Ruby Shell when Hello World is assigned to variable a and the content of variable a is then printed on the IRB.

Figure 1.1.1: Ruby IRB

1.2 Create And Run A Script

A Ruby script is a file that ends with the extension .rb. Create a file named hello.rb and open the file with your text editor and insert the Hello World code below. Figure 1.2.1 shows the code shown in Table 1.2.1 is inserted into the file created.

Table 1.2.1: File to create and code to insert


print “hello world\n”

Figure 1.2.1: Hello World code in text editor

To run the Ruby script that you have created, use the command ruby YOUR_FILE_NAME.rb. Depending on the location where you save your file, you may have to insert the complete path of the file.

Figure 1.2.2: Result of executing hello.rb script in terminal window

Figure 1.2.2 shows the Ruby script that I have created is located at C:\Ruby22-x64\code\intro and hello world is printed on the terminal window.

1.3 Print and Comment

There are two ways of printing a statement from the script: print and puts. Both of the method will have the same output result. The symbol # is used for single line comment while =begin and =end are used for multiple line comment. Commented code will not be executed as they are for comment purpose.

Table 1.3.1: Two different printing method


#single line comment

    multiple line comment
    multiple line comment

print “hello world”
puts “hello world”

Figure 1.3.1: Result of executing print.rb script in terminal window

Figure 1.3.1 shows the output where the code from Table 1.3.1 is executed. Both of the method shows the same result and code in comment will not be printed.

1.4 Variable Scope and Checking

In Ruby, there are has five types of variable scope: local, global, instance, class, and constant type. Table 1.4.1 shows each of the Ruby variable scope and how they are declared. Each variable is began by declaring using using the special character shown in the table.

Table 1.4.1: Ruby variable scope

Name Begins With Variable Scope
$ A global variable
@ An instance variable
[a-z] or _ A local variable
[A-Z] A constant
@@ A class variable

Local variable and instance variable are widely used in Ruby on Rails programming. To check the scope of a Ruby variable, we can use the defined? method. Table 1.4.2 shows a simple script that is written to check the variable scope.

Table 1.4.2: Two different printing method


hello = “bla?”

puts defined? hello

Figure 1.4.1 shows the output where the code from Table 1.4.2 is executed. The type of the variable scope is defined and shown on the terminal window.

Figure 1.4.1: Result of executing variable.rb script in terminal window

1.5 While Loop

While loop in Ruby is straightforward. Table 1.5.1 shows a sample while loop statement and Figure 1.5.1 shows the result when the script is executed.

Table 1.5.1: While Loop


a = 0

while (a < 3)  
  puts "#{a\1}\n"  
  a = a\1  

Figure 1.5.1: Result of executing while.rb script in terminal window

1.6 Array and Loop

Table 1.6.1 shows an sample array and loop statement and Figure 1.6.1 shows the result when the script is executed.

Table 1.6.1: Array and Loop


a = [100,"hello world",-9.9, 3.142]

a.each do |b|  
  print "#{b}\n"  

Figure 1.6.1: Result of the while.rb script on terminal window

1.7 Fixnum Loop

There are two methods we can use for Fixnum Loop: upto and downto. It will print ascendingly or descendingly within the range you assigned and based on method you use. Table 1.7.1 shows a sample fixnum loop and Figure 1.7.1 shows the result when the script is executed.

Table 1.7.1: Fixnum Loop


8.upto(11) {|a| print "#{a}\t"}  
puts "\n"  
5.downto(2) {print "Hello World\t"}

Figure 1.7.1: Result of executing fixnum.rb script on terminal window

1.8 For Loop

For Loop and Fixnum Loop are very similar. Generally, Ruby’s For Loop syntax is similar to other programming language. Table 1.8.1 shows a sample For Loop and Figure 1.8.1 shows the result when the script is executed.

Table 1.8.1: For Loop


for a in [99, 100, 101, -102, "sandwich"]  
    puts "#{a}"  

puts "\n"

for b in (1000..1004)  
    print "#{b}\t"  

Figure 1.8.1: Result of executing for.rb script in terminal window

1.9 Recursive

Ruby’s recursive syntax is similar to other programming language. Table 1.9.1 shows a sample Recursive program and Figure 1.9.1 shows the result when the script is executed.

Table 1.8.1: For Loop


def decrease(n)  
  return if  
  puts n  


Figure 1.9.1: Result of executing recursive.rb script in terminal window

1.10 If, Else, Else If Statement

Ruby’s syntax for If Else Elsif are different compared to other programming language. Do not that the Else If statement is spelled as Elsif. Table 1.10.1 shows a sample If, Else, Else If program and Figure 1.10.1 shows the result when the script is executed.

Table 1.10.1: If, Else, Else If Statement


a = 3  

if a == 1  
 puts "Meow"  
elsif a == 2  
 puts "Woof"  
elsif a == 3  
 puts "Moo"  
 puts "Haha"  

Figure 1.10.1: Result of executing if_else.rb script in terminal window

results matching ""

    No results matching ""