SL Unit 1 Ruby

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 92

What is a Programming Language?

 "A programming language is a way by which


programmers communicate with computers through the
set of instructions known as code/program.“
 Programming languages are the computer languages that
are used in computers to provide instruction and
implement algorithms. Each programming language
contains its own set of rules for writing the code, and such
rules are known as Syntax.
• Some popular programming languages are
• C,C++, Pascal, COBOL
• Java (But java is compiled and interpreted as firstly its source
code is compiled into byte-code, and then interpreted at runtime).
Advantages:
•These are building blocks for other computer
languages.
•These are well suited for large projects.
Applications of Programming languages
•Programming languages are mainly used to
create different software and applications such
as MS Excel, PowerPoint, etc.
•These are used for transforming the data, for
example, solving a set of equations from a set of
conditions.
What is a Scripting Language?
A scripting language is a type of programming language which
does not require explicit compilation step, and it is designed for a
runtime system to automate the execution of tasks
Scripting languages support "script," which is small program
written for a specific runtime environment. These are interpreted
at runtime rather than compiled. It means, to convert the source
code to machine code, scripting languages use an interpreter, not
the compiler.
The scripting language refers to dynamic high-level, general-
purpose interpreted languages such as Python, Perl, etc. Thus, a
scripting language can automate different environments such
as application software's, webpages, text editors, operating system
shells, computer games, etc.
Ex: The scripting language refers to dynamic high-level, general-
purpose interpreted languages such as Python, Perl, etc.
a scripting language can automate different environments such
as application software's, webpages, text editors, operating system shells,
computer games, etc.
Advantages:
•It is an easy and quick process to learn coding in Scripting language,
and for this, much knowledge of web technology is not needed.
•In scripting languages, a wide variety of libraries is available that
enable the developers to develop new applications.
•With the help of scripting languages, we can add visualization
interfaces and combinations to web pages. Most of the latest web pages
need scripting languages for creating enhanced web pages, fascinating
UI, and many more.
•There are less number of data structures and variable to be used,
which make it highly efficient.
•These are Less code-intensive as compared to traditional
programming languages.
Applications of Scripting Language
•These are used to automate a specific task in a program.
•These are useful to extract information from a dataset.
Programming Language Scripting Language
A programming language is a computer language A scripting language is a type of
that is used to communicate with computers using programming language designed for a
a set of instructions. runtime system to automate the execution of
tasks.
It is compiled language or compiler-based It is interpreted language or interpreter-
language. based language
It is used to develop an application or software It is used to combine existing components and
from scratch. automate a specific task.
It runs or executes independently and does not It runs or executes inside another program.
depend on the parent (exterior) program.
It uses a compiler to convert source code into It uses an interpreter to convert source code
machine code. into machine code.
As it uses a compiler, hence the complete program As it uses an interpreter, hence the program
is converted into machine code in one shot. is converted into machine code line by line.
These languages are required to be compiled. There is no need for compilation.
It is comparatively difficult to write code in a It is comparatively easy to write code in the
programming language, and it requires numerous scripting language, and it requires few lines
lines of code for each task. of code for each task.
The development time in programming languages The development time in a scripting language
is high as more lines are required. as a smaller number of lines are required.
Ruby Variables
•Ruby variables are locations which hold data to be used
in the programs. Each variable has a different name.
These variable names are based on some naming
conventions. Unlike other programming languages, there
is no need to declare a variable in Ruby. A prefix is
needed to indicate it.
•There are four types of variables in Ruby:
Local variables
Class variables
Instance variables
Global variables
Local variables
A local variable name starts with a lowercase letter or
underscore (_). It is only accessible or have its scope within the
block of its initialization. Once the code block completes,
variable has no scope.
When uninitialized local variables are called, they are
interpreted as call to a method that has no arguments.
Class variables
•A class variable name starts with @@ sign. They need to be
initialized before use. A class variable belongs to the whole
class and can be accessible from anywhere inside the class. If
the value will be changed at one instance, it will be changed at
every instance.
•A class variable is shared by all the descendents of the class.
An uninitialized class variable will result in an error.
• Example:
• #!/usr/bin/ruby
Class States
@@no_of_states=0
def initialize(name)
@states_name=name
@@no_of_states += 1
end
def display()
puts "State name #@state_name"
end
def total_no_of_states()
puts "Total number of states written: #@@no_of_states"
end
end
# Create Objects
first=States.new("Assam")
second=States.new("Meghalaya")
third=States.new("Maharashtra")
fourth=States.new("Pondicherry")
# Call Methods
first.total_no_of_states()
second.total_no_of_states()
third.total_no_of_states()
fourth.total_no_of_states()
In the above example, @@no_of_states is a class variable.
 An instance variable name starts with a @ sign. It belongs to one instance of
the class and can be accessed from any instance of the class within a method.
They only have limited access to a particular instance of a class.
 They don't need to be initialize. An uninitialized instance variable will have a
nil value.
Sample Program:
class States
def initialize(name)
@states_name=name
end
def display()
puts "States name #@states_name"
end
end
# Create Objects
first=States.new("Assam")
second=States.new("Meghalaya")
third=States.new("Maharashtra")
fourth=States.new("Pondicherry")
# Call Methods
first.display()
second.display()
third.display()
fourth.display()
Out put:
Global variables
A global variable name starts with a $ sign. Its scope is globally, means it can be
accessed from any where in a program.
An uninitialized global variable will have a nil value. It is advised not to use them as
they make programs cryptic and complex.
There are a number of predefined global variables in Ruby.
Example:
$global_var = "GLOBAL"
class One
def display
puts "Global variable in One is #$global_var"
end
end
class Two
def display
puts "Global variable in Two is #$global_var"
end end
oneobj = One.new
oneobj.display
twoobj = Two.new
oneobj = One.new
oneobj.display
twoobj = Two.new
twoobj.display
In the above example, @states_name is the instance variable.
Local Global Instance Class

Scope Limited within Its scope is It belongs to one Limited to the


the block of globally. instance of a whole class in
initialization. class. which they are
created.

Naming Starts with a Starts with a $ Starts with an @ Starts with an


lowercase letter sign. sign. @@ sign.
or underscore
(_).
Initializatio No need to No need to No need to They need to be
n initialize. An initialize. An initialize. An initialized before
uninitialized uninitialized uninitialized use. An
local variable is global variable instance variable uninitialized
interpreted as will have a nil will have a nil global variable
methods with no value. value. results in an
arguments. error.

You might also like