Ruby/C2/Hello-Ruby/English
Title of script: Hello Ruby!
Author:Afrin Pinjari
Keywords: Ruby,video tutorial, helloworld, puts, print
|
|
---|---|
Slide 1 | |
Slide 2
Learning Objectives
|
In this tutorial we will learn
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Pre-requisites |
To follow this tutorial you must be connected to the internet.
You must have knowledge of using Terminal and Text editor in Linux. |
Slide 5
|
Now I will explain what is Ruby.
|
Slide 6
|
Now let us see some features of Ruby.
|
Slide 7
|
One of the most important feature of Ruby is RubyGems.
For more information on RubyGems visit the following link.
|
Slide 8
|
To get more help on Ruby you can visit the links shown. |
Slide 9
|
You can install Ruby using the Ubuntu Software Centre.
For more information on Ubuntu Software Centre, please refer to the Ubuntu Linux Tutorials on this website.
|
Slide 10 | Other methods for installing Ruby are as shown in this slide. |
Slide 11
|
Ruby code can be executed in 3 ways
|
First let us see how to execute 'Hello World' code from the command line. | |
Switch to Terminal
Press Ctrl+Alt+t |
Open a terminal by pressing the Ctrl, Alt and T keys simultaneously.
|
Type
ruby -e 'puts ”Hello World”'<<press Enter |
Type the command
ruby space hyphen e space within single quotes puts space then within double quotes Hello World and
|
Highlight Hello world | We get the output as Hello World. |
Highlight puts | puts command is used to print the output on the terminal. |
Highlight -e | The hyphen e flag allows only a single line of code to be executed.
Multiple hyphen e flags can be used to execute multiple line commands.
|
Press Up Arrow key | Now press the up Arrow key to get the previous command and |
Type
ruby -e 'puts “Hello World”' -e 'puts 1+2'<< Press Enter
|
Type
space hypen e space within single quotes puts space 1+2 and
|
Highlight
Hello World and 3 |
We get the output as Hello World and 3.
|
Slide 11
|
We will now learn about Interactive Ruby.
|
Switch to the Terminal
|
Now let us execute our Ruby code through irb.
Go to the terminal
irb and Press Enter. To launch Interactive Ruby |
Type puts ”Hello World” <<Press Enter | Type puts space within double quotes Hello World and press Enter. |
Highlight Hello World and nil
|
We get the output as Hello World.
We get the return value as nil.
|
Type exit <<Press Enter | To exit from irb type exit and press Enter. |
You can also run Ruby program from a file.
| |
Switch to text editor | I am using gedit text editor.
Let me switch to the gedit text editor. |
Type puts ”Hello World ” | Now, type puts space within double quotes Hello World |
Lets learn how to add multiple line or block comments. | |
Type =begin <<press Enter | Before the puts command
Type, equal to begin and press Enter |
Highlight =begin | Equal to begin is used to start the comment. |
Type
This code will print Hello world Press Enter |
Type the comments that you wish to add.
This code will print Hello world. Press Enter |
Type
=end |
Now type
equal to end |
Highlight =end | equal to end is used to end multiple line comments. |
Comments are useful to understand the flow of program.
It is useful for documentation. | |
Click on the Save button | Now, let us save the file by clicking on the Save button.
|
In the Save As dialog box >> Browse the location to save the file | The Save As dialog box appears on your screen.
Browse the location where you want to save the file. |
Create a folder named rubyprogram on the Desktop. | On the Desktop, I will create a folder named rubyprogram.
|
In Name text-box >> type
hello.rb |
In the Name text-box, type the name that you wish to add.
I will type hello.rb |
Point to Dot rb extension | Dot rb extension is given to a Ruby file |
Click on Save button | Then click on Save button to save the file.
So the file is saved now. |
Switch to the terminal.
|
To execute the code, go to the terminal.
Lets 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 subdirectory rubyprogram. | |
Type cd Desktop/rubyprogram<< Press Enter | To do so, type cd space Desktop/rubyprogram and press Enter. |
Type ruby hello.rb <<Press Enter | Let's execute the file
Type ruby space hello dot rb and press Enter |
Highlight HelloWorld | We get the output HelloWorld. |
Now let me demonstrate the difference between puts and print statement.
| |
Type
cd <<press Enter |
Before that we need to go back to the home directory
To do so type cd and press Enter |
In the terminal
Type irb<< press Enter |
Now type
irb and Press Enter to launch the Interactive Ruby. |
Type puts “Hello”,”World” | Type
puts space within double quotes Hello comma within double quotes World |
Highlight or point to comma
Press Enter |
Here comma is used to join the two puts command together.
Now press Enter. |
Highlight Hello World | We get the output Hello World, but on separate lines. |
Type print “Hello”,”World”
<<Press Enter
|
Now let's try the same thing with print.
Replace puts with print Press Enter. |
Highlight HelloWorld | 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. | |
<<Pause>>
This brings us to the end of this Spoken Tutorial. | |
Slide 11
|
In this tutorial we have learnt
=begin ...... =end
|
Slide 13
Assignment |
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 Hint: #............... |
Slide 14
About the Spoken Tutorial Project |
Watch the video available at the following link.
If you do not have good bandwidth, you can download and watch it. |
Slide 15 | The Spoken Tutorial Project Team :
Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Slide 16
|
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 below link: spoken-tutorial.org/NMEICT-Intro. |
NO SLIDE
On the previous slide itself. |
This is Afrin Pinjari from IIT Bombay, signing off.
Thank you for watching. |