Professional Documents
Culture Documents
Introduction to Ruby
A dynamic, open source programming language with a focus on simplicity
and productivity. It has an elegant syntax that is natural to read and easy to
write. Ruby is also an object oriented language.
Installation
Installation on Windows
Installation on Mac
Installation on Windows:
To download Ruby, go to rubyinstaller.org/downloads
Download the latest version of Ruby. The one which is in the top most.
For 64 bit Operating Systems, download installer that end with (x64).
After downloading the exe file, double click to open the file.
Choose the language.
Browse and select the desired installation path you want. But it is
recommended to install on the default directory. And Check the 3 options below
Installation on Mac:
Ruby has already installed on Macintosh.
So dont need to go through installation procedures.
As you can see in the above screen, we can write ruby expressions and
statements.
The texts entered within quotes are called as String.
We can perform concatenation operation with two strings.
We can store the value to the variable like
name = Welcome to Ruby!!!
Ruby is a case sensitive programming language. So, name is different
from Name and NAME is different from both.
We can also perform some complicated operations. See the below screens.
Whoa!! What just had happened? You might be confused after seeing this.
I said it prints 1 to 9. But its showing all 1s that too many. To get out of this,
press Ctrl + C
This is because the variable i is not incremented.
The following code prints from 1 to 9
i=1
while i < 10
print i
i += 1
end
This code prints from 1 to 9.
We can also define functions in irb.
A function is a set of statement(s) to perform a particular task.
Here is a function that performs square of a number.
def square(n)
return n * n
end
Calling the function: square(4)
After calling this function by passing 4 as argument, it returns 16 by
performing the operation 4 * 4.
Likewise, a function to find the area of a circle.
def circle(radius)
return radius * radius * 3.14
end
Calling the function: circle (5) by passing the argument as 5.
This function returns 78.5 by multiplying 5 * 5 * 3.14
Ruby Scripts
We are going to run ruby from a text editor rather that a Command
Prompt.
You can use any text editor like Notepad or Notepad++.
Ruby scripts have .rb extension. So, you have to save the file with the
extension .rb
Now, in your editor enter the following code and save it with any name
with .rb extension:
puts Welcome to Ruby!!!
Im saving this as first.rb. To run the program, go to Command Prompt,
and type ruby first.rb without quotes.
You can actually run by simply typing first.rb, because during installation,
you have checked Add Ruby executables to your PATH. So, its enough to type
first.rb for running the program.
Likewise, you can use any text editor in Macintosh and follow the same
procedures as in Windows.
Lesson - 2
Displaying Data
Now, we can see how to display data to the screen.
To display text, we have two functions
puts()
print()
You can also display the numeric data using these functions.
puts(1)
print(1)
Also you can evaluate the expression and display the result.
puts (2+2)
print(2+2)
Getting Data
To make the program to interact with user, we have to get the input data
from the users.
To do this, we can use gets() method.
gets() function gets input from the keyboard in string format and stores
the value in the variables.
name = gets
This statement gets a string input from the user and stores it in the
variable called name.
It also appends a \n new line character at the end of the input entered by
the user.
So, when you type the variable name, it displays what it is stored in it.
When you display using print function, it displays the text entered by the
user along with a new line.
If you want to remove the newline, you can use a function called chomp
The following statement removes the newline from the input entered by
the user.
print name.chomp
The following code performs addition of two whole numbers with the
numbers you have got from the user:
number1 = gets
number 2 = gets
(Integer)number1 + Integer(number2)
Integer() part of the code looks tricky for you now.
As said before gets method gets input in string format. So, you have to
convert it to whole number. This can be done using Integer() function.
If you dont convert those variables into integer and when you try to
perform addition, it concatenates two string. Do you remember concatenating
two strings which we have seen earlier in Chapter 1?
Likewise, to add two decimal numbers, look the following code:
num1 = gets
num2 = gets
Float(num1) + Float(num2)
Float() method is used to convert data into decimal format. If the
conversion is not done, it simply performs the concatenation operation as said
before.
Lesson 3
Data types
In this lesson, we are about to see different data types.
Another way to create string is using gets method, which we have seen in
last chapter.
As you already know, using gets it automatically inserts new line at the
end.
HERE:
To create a multiline string and store it in a variable HERE keyword is
used.
This begins with a two less than symbols << and the keyword HERE
indicates that, until you see the keyword HERE again, store all the values in the
variable.
first,last = Bharath,Kumar.split(/,/)
Here , is the delimiter. We use slashes (/) to delimit the delimiters.
When this statement is executed, the piece of text before comma (,) is
assigned to the variable first and the piece of text after comma (,) is assigned
to the variable last.
Therefore, value Bharath is assigned to the variable first and value
Kumar is assigned to the variable last.
Squeeze method:
Squeeze is used to remove trailing spaces from the string.
In the above image, you could note the number of spaces in the variable
first.
When printing the first variable using squeeze method it removes the
trailing spaces.