Ruby/C2/Variables-in-Ruby/English

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

Title of script: Variables in Ruby

Author: Afrin pinjari

Keywords: to_s, to_i, video tutorial, local variable, global variable


Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Variables in Ruby
Slide 2

Learning Objectives

In this tutorial we will learn
  • What is a variable?
  • Dynamic typing in Ruby
  • Declaring a variable
  • Converting variable types
  • What is variable's scope?
  • Types of variables
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 have knowledge of using Terminal in Linux.

You must also be familiar with irb

If not, for relevant tutorials, please visit our website

Slide 5

Variables

Now I will explain what a variable is.
  • Variable is used to store a value.
  • Variable is a reference that can be assigned.
  • Please note that Ruby variables are case sensitive.
Slide 6

Variables

* Variable names should be meaningful.
  • Variable name may only contain lowercase letters, numbers and underscores.
  • Ex : first_name
Slide 7

Dynamic Typing

Now let us see what is dynamic typing
  • Ruby is a dynamic typed language.
  • It means that you don't need to declare datatype while creating a variable.
  • Ruby interpreter determines the data type at the time of assignment.
Now let us see how to declare a variable in Ruby.
Press Ctrl+Alt+t

Switch to the terminal

Open the terminal by pressing Ctrl,Alt and T keys simultaneously.

A terminal window appears on your screen.

Type

irb<<Press Enter

Type irb

Press Enter to launch Interactive Ruby

Type

var1=10 <<press Enter

Now type var1 equal to 10

Press Enter

Highlight var1

Highlight 10

Here we have declared a variable var1 and assigned a value 10 to it.
Let's check whether the datatype allotted by the interpreter is integer or not.
Type

var1.kind_of?Integer << press Enter

So, type

var1 dot kind underscore of question mark Integer

and press Enter

Highlight true We will get the output as true.
In Ruby you can dynamically change the variable type.

To do so, just assign a new value to it.

Let's do this by assigning a string value to variable var1.

Type

var1=”hello”<< press Enter

Type

var1 equal to within double quote hello

press Enter

Type

var1.class<< press Enter

Let's verify the variable type assigned

Type var1 dot class

Highlight .class Class method tells us what class of variable it is and

Now Press Enter

Highlight string We get the output as string
Ruby has automatically changed the variable type from integer to string
We will now learn how to convert a variable value to a different type.

Let's switch back to the slides

Slide 7

Converting variable types

Highlight to_s

Ruby variable classes have methods to convert their value to a different type
  • to_i method is used to convert a variable to integer
  • to_f method is used to convert a variable to floating point value
  • to_s method is used to convert a variable to string


The to _ s() method takes number base as an argument.

The conversion depends on this number base.

Press Ctrl+L Now let us try out these methods.

Go to the terminal

Let's clear the terminal first.

Press Ctrl + L to clear the irb console

Type

y=20<<press Enter

Now type y equal to 20 and press Enter
Highlight y and 20 Here we have declared a variable called y and assigned a value 20 to it.

We will now convert y to a floating point value using to underscore f method

Type

y.to_f<< press Enter

Type y dot to underscore f and press Enter
Highlight 20.0 We will get the value as float.
Type

y.to_s<< press Enter

Now type y dot to underscore sand press Enter
Highlight “20” We will get output 20 within double quotes
To convert variable y in binary form give number base as 2 in to_s method
Press Up Arrow Press up arrow key to get the previous command
Type y.to_s(2)<<press Enter Now type opening bracket 2 closing bracket and press Enter
Highlight 10100 We will get the output 10100 in the binary form
Similarly you can convert variable y to octal or hexadecimal form

By changing the number base to 8 or 16.

Let us switch back to our slide
Slide 8

Variable Scope

We will now learn what is variable scope.

Scope defines where in a program a variable is accessible.

Ruby has four types of variable scope:

  • Local
  • Global
  • Instance
  • Class
Slide 9

Naming Convention

Each variable type is declared by using a special character

At the beginning of the variable name

Name Beginning With Variable Scope

$ A global variable

[a-z] or _ A local variable

@ An instance variable

@@ A class variable

[A-Z] A constant


$ represents global variable

Lower case letters and underscore represents a local variable

@ represents an instance variable

Two @@ represents a class variable

Upper case letters represents a constant

We will learn in detail about this in another tutorial.
<<Pause>>

This brings us to the end of this Spoken Tutorial.let us summarise

Slide 11

Summary

In this tutorial we have learnt
  • To declare a variable
    eg var1=10
  • Changing variable type using to_f, to_s methods
  • Different Variable scope
Slide 13

Assignment

As an assignment

Declare a variable and convert it to octal and hexadecimal form

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 at spoken hyphen tutorial dot 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:

spoken hyphen tutorial dot org slash NMEICT hyphen Intro.

This is Afrin Pinjari from IIT Bombay, signing off.

Thank you for watching.

Contributors and Content Editors

Afrin, Nancyvarkey, Sneha