Welcome to the Spoken Tutorial on Hello Ruby!.
In this tutorial, we will learn
What is Ruby?
Features, RubyGems & Help on Ruby
Installation, Running Ruby code
Commenting, Difference between puts and print.
Here, we are using: Ubuntu Linux version 12.04, Ruby 1.9.3
To follow this tutorial, you must be connected to internet.
You must have the knowledge of using Terminal and Text editor in Linux.
Now, I will explain you what is Ruby.
Ruby is an object-oriented, interpreted scripting language.
It is dynamic, open source programming language.
It has an elegant syntax that is natural to read and easy to write.
Now, let us see some features of Ruby.
Ruby is highly portable.
Ruby program runs in any operating system.
Variables in Ruby have no datatype such as in Smalltalk, BASIC or Python.
It supports automatic memory management.
Ruby is a free format language.
You can start writing your program from any line and column.
Ruby is used for developing Internet and Intra-net applications.
One of the most important features of Ruby is RubyGems.
RubyGems is a package manager for Ruby programming language.
It provides a standard format for distributing Ruby programs and libraries.
You can create and publish your own gems.
For more information on RubyGems, visit the following link.
To get more help on Ruby, you can visit the links shown.
You can install Ruby using Ubuntu Software Centre.
For more information on Ubuntu Software Centre, please refer to the Ubuntu Linux Tutorials on this website.
Other methods for installing Ruby are as shown in this slide.
Ruby code can be executed in 3 ways-
Command line , Interactive Ruby and
As a file. We will go through each method of execution.
First, let us see how to execute the Hello World code from command line.
Open a terminal by pressing the Ctrl, Alt and T keys simultaneously.
A terminal window appears on your screen.
Type the command: ruby space hyphen e space within single quotes puts space then within double quotes Hello World and
press Enter.
We get the output as "HelloWorld".
puts command is used to print the output on the terminal.
The hyphen 'e' flag allows only a single line of a code to be executed.
Multiple hyphen 'e' flags can be used to execute multiple line commands.
Let's try this out.
Now, press the up-arrow key to get the previous command and
type: space hyphen e space within single quotes puts space 1+2 and
press Enter.
We get the output as "Hello World" and 3.
Let's switch back to our slide.
We will now learn about Interactive Ruby.
Interactive Ruby allows the execution of Ruby commands with immediate response.
You can run Ruby statements and examine the output and the return values.
For older version of Ruby, install the irb separately.
Now, let us execute our Ruby code through 'irb'. Go to the terminal.
Type "irb" and press Enter
to launch the Interactive Ruby.
Type: puts space within double quotes Hello World and press Enter.
We get the output as Hello World
and we get the return value as nil.
To exit from irb , type exit and press Enter.
You can also run the Ruby program from a file.
You can use any text editor of your choice to write the code.
I am using gedit text editor. Let me switch to gedit text editor.
Now, type: puts space within double quotes Hello World .
Let's learn how to add multiple line or block comments.
Before the puts command,
type: equal to begin and press Enter.
equal to begin is used to start the comment.
Type the comments that you wish to add.
I will type: "My first Ruby program"
and press Enter.
Then type: "This code will print helloworld" and press Enter.
Now, type: equal to end.
equal to end is used to end the multiple line comments.
Comments are useful to understand the flow of program.
It is useful for documentation.
Now, let us save the file by clicking on the Save button.
It is a good practice to save the file frequently.
The Save As dialog-box appears on your screen.
Browse the location where you want to save the file.
On Desktop, I will create a folder named "rubyprogram".
We will save the file inside this folder.
In the Name text-box, type the name that you wish to add.
I will type "hello.rb".
dot rb extension is given to a Ruby file.
Then click on the Save button to save the file. So, the file is saved now.
To execute the code, go to the terminal.
Let's clear the terminal first.
Make sure that you are in the directory where your Ruby file is present.
Remember that we are in the home directory. We need to go to the sub-directory rubyprogram.
To do so, type: cd space Desktop/rubyprogram and press Enter.
Let's execute the file. Type: ruby space hello dot rb and press Enter.
We get the output as "HelloWorld".
Now, let me demonstrate the difference between puts and print statements.
We will try this out using "irb".
Before that, we need to go back to our home directory. To do so, type "cd" and press Enter.
Now, type "irb" and press Enter to launch the Interactive Ruby.
Type: puts space within double quotes Hello comma within double quotes World .
Here, comma is used to join the two puts commands together.
Now, press Enter.
We get the output Hello, World, but on the separate lines.
Now, let's try the same thing with print.
Press up-arrow key to get the previous command.
Replace puts with print and press Enter.
We get the output as Hello World but on the same line.
The keyword puts adds a newline to the end of the output. The keyword print does not.
The keyword print outputs only what we have provided.
This brings us to the end of this Spoken Tutorial. Let us switch back to our slide.
In this tutorial, we have learnt:
About Ruby, Installation
Execution of Ruby code
Adding multiple comments using =begin and =end
Difference between puts and print.
As an assignment,
write a program to print your name and age.
We used multiple line comments in this tutorial.
Try to give single line comment.
Watch the video available at the following link.
It summarizes the Spoken Tutorial project.
If you do not have good bandwidth, you can download and watch it.
The Spoken Tutorial Project team:
Conducts workshops using spoken tutorials.
Gives certificates to those who pass an online test.
For more details, please write to: contact@spoken-tutorial.org
Spoken Tutorial project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
More information on this mission is available at the below link.
This is Afrin Pinjari from IIT Bombay, signing off.
Thank you for watching.