Ruby/C2/Hello-Ruby/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Hello Ruby!

Author:Afrin Pinjari

Keywords: Ruby,video tutorial, helloworld, puts, print


Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Hello Ruby!
Slide 2

Learning Objectives

In this tutorial we will learn
  • What is Ruby?
  • Features
  • RubyGems & Help on Ruby
  • Installation
  • Running Ruby code
  • Commenting
  • Difference between puts and print
Slide 3

System Requirements

Here we are using
  • Ubuntu Linux version 12.04
  • Ruby 1.9.3
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


What is Ruby?

Now I will explain 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.
Slide 6

Features

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 free format language.
  • You can start writing your program from any line and column.
  • Ruby is used for developing Internet and Intra-net applications.
Slide 7

RubyGems

One of the most important feature of Ruby is RubyGems.

RubyGems is a package manager for the 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.

Slide 8

Help on Ruby

To get more help on Ruby you can visit the links shown.
Slide 9

Installation

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

Running Ruby

Ruby code can be executed in 3 ways
  • Command line
  • Interactive Ruby
  • As a file

We will go through each method of execution.

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.

A terminal window appears on your screen.

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

Press Enter.

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.

Lets try this out

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

Press Enter.

Highlight

Hello World and 3

We get the output as Hello World and 3.

Let's switch back to our slide

Slide 11

Interactive Ruby

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 return values.

For older version of Ruby, install irb separately.

Switch to the Terminal

Type irb<< press Enter

Now let us execute our Ruby code through irb.

Go to the terminal.

Type 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.

And 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.

You can use any text editor of your choice to write the code.

Switch to text editor I am using gedit text editor.

Let me switch to 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

My first Ruby program

This code will print Hello world

Press Enter

Type the comments that you wish to add.

I will type

My first Ruby program. Press enter

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.

It is a good practice to save the file frequently.

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 Desktop, I will create a folder named rubyprogram.


We will save the file inside this folder.

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.

We will try this out using irb

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.

Press up arrow key to get the previous command

Replace puts with print and

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


Summary

In this tutorial we have learnt
  • About Ruby
  • Installation
  • Execution of Ruby code
  • Adding multiple comments

=begin

......

=end

  • Difference between puts and print
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.


It summarises the Spoken Tutorial project.

If you do not have good bandwidth, you can download and watch it.

Slide 15 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

Slide 16

Acknowledgement

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.

Contributors and Content Editors

Afrin, Nancyvarkey, Sneha