0% found this document useful (0 votes)
121 views47 pages

Ruby

Ruby is an object-oriented scripting language created by Yukihiro Matsumoto. It is inspired by Perl, Smalltalk, and Lisp. Some key features of Ruby include being fully object-oriented, dynamically typed, and supporting metaprogramming. Variables in Ruby do not require declaration and scopes are determined by name. Common data types include numbers, booleans, strings, arrays, and hashes. Arrays and hashes can be used to store collections of data.

Uploaded by

krishna kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views47 pages

Ruby

Ruby is an object-oriented scripting language created by Yukihiro Matsumoto. It is inspired by Perl, Smalltalk, and Lisp. Some key features of Ruby include being fully object-oriented, dynamically typed, and supporting metaprogramming. Variables in Ruby do not require declaration and scopes are determined by name. Common data types include numbers, booleans, strings, arrays, and hashes. Arrays and hashes can be used to store collections of data.

Uploaded by

krishna kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

RUBY

• Ruby is an object-oriented programming


language developed by Yukihiro Matsumoto.
Ruby is a dynamic programming language with a
complex but at the same time expressive
grammar.

• Ruby is inspired by other low level and object


oriented programming languages
like Lisp, Smalltalk, and Perl and uses syntax that
is easy for C and Java programmers to learn.
Features of Ruby
• Object Oriented : Every values in Ruby is an
object, even the most primitive things like
strings, numbers and even true and false. So,
Ruby is a pure Object Oriented Language.
Every object has a class and every class has
one superclass.
• Dynamic : Ruby is a very dynamic
programming language. Ruby programs aren't
compiled like C or Java programs. All things in
a program are built by the code when it is run.
A program can also modify its own definitions
while running.
• Singleton Classes : Every object in Ruby has two
classes: a regular class and a singleton class. An
object's singleton class is nameless class whose
only instances is that object. Singleton classes are
created automatically and make Ruby simple and
elegant.
• Metaprogramming : Everything in Ruby are
objects. You can use them to learn about them or
even modify them, while your program is
running. This technique is called
metaprogramming.
• Flexibility : Methods can be added to existing
classes without sub classing, operators can be
overloaded, and even the behaviour of the
standard library can be redefined at runtime.
• Variables and scopes : Programmer do not
need to declare variables or variable scope in
ruby. The name of the variable automatically
determines its scope.
• Examples :
• Var is a local variable.
• $var is a global variable.
• @var is an instance variable.
• @@var is a class variable.
Ruby: Advanced Features

• Exceptions for handling errors.


• Garbage Collector.
• OS - independent threading, which allows you
to write multi-threaded applications even on
operating systems such as DOS.
• You can write extensions to Ruby in C.
Why you should go for Ruby?
• Ruby is a server-side scripting language.
• Ruby can be embedded into HTML.
• Ruby has similar syntax to that of many
programming languages like C and Java.
• Ruby supports mostly all the platforms like
Windows, Mac and Linux.
• Ruby can be easily connected to Oracle,
MySQL, DB2.
Ruby Scripts

• We are going to run ruby from a text editor


rather that a Command Prompt. You can use
any text editor
like Notepad, Notepad++, Sublime, Brackets.
Ruby scripts have .rb extension. So, you have
to save the file with the extension .rb. Now, in
your editor enter the following code and save
it with any name with .rb extension:
puts "Welcome to Ruby!"
• Let's save this as first.rb. To run the program,
go to Command Prompt, and type ruby
first.rb. You can also run by simply
typing first.rb as well, if during
installation, "Add Ruby executables to your
PATH" option was checked.
Comments in Ruby

• Ruby: Single line Comment


• Single line comment is created
using #(Hash) sign in Ruby.
• print “Hello World” #Used to Display the Text

• Whenever, Ruby finds # on a line, everything


after that will be ignored.
Ruby: Multiple line Comment

• Multiple line comments are created using the


code blocks =begin / =end
• =begin This is used to illustrate multiple line
comments in Ruby language. =end

• The statements inside this block are ignored


by the Ruby Interpreter.
Display Data using print() and puts()

• puts()
• print()

• Ruby: puts() method


• puts adds a new line automatically at the end of
the data.
• puts ("hello, world")
• puts "hello, world“
• puts "hello, world", "Goodbye, world"
Ruby: print() method
• print doesn't add a new line at the end.
• print "hello, world“
• If you want to include new line at the end of
the data using print method, then you have to
include special character \n for new line.
• print "hello, world\n"
• print "practice\n", "makes\n", "a\n", "man\n",
"perfect\n"
Getting Data from User using gets in
Ruby
• To make the program to interact with user, a
program usually needs input from the users.
To do this, we can
use gets method. gets function takes input
from the keyboard in string format and stores
the value in the variables.
• name = gets
• number1 = gets
• number2 = gets
• (Integer)number1 + Integer(number2)
Variables and Data types in Ruby
• number = 100
• Ruby: Rules for naming variables:
• Must begin with lowercase letters or underscore
like : number, _name.
• Underscore come in handy when we create a
compound variable using Snake Case like,

• first_name = "Bharath“

• Another way to write compound variable or


multiword variable is Camel Case.

• lastName = "Kumar"
• Followed by the underscore or lowercase letter the
next character of a variable can be a number or other
letters, but not symbols.
• _123, rate1 //valid
• #abc, n*ame //invalid variable names.

• Names should match any of the reserved keywords.

• Global variables are created by putting a dollar


symbol $ before the variable name.
• Ex. $salary = 10000.
• salary is a global variable which can be seen
throughout the program.
Ruby: Number Datatype

• Ruby supports major number systems such as


whole numbers, floating point numbers. Different
bases such as Binary, Octal and Hexadecimal are
allowed.
• To write in binary we use 0b and the
corresponding number. For example, 5 can be
written as 101 in binary. Likewise, to represent
number in hexadecimal 0x is used and for
octal 0 alone is used. Negative numbers are
represented by using unary operator -
(minus) before the number.
• Ruby supports different arithmetic operations
such as addition, subtraction, multiplication,
division, modulus and exponentiation.
Ruby: ABS Method

• abs is a method used to find the absolute


value of a number. For example, -26.absgives
the value of 26. Some other methods
are div and modulo.
• You can also convert numbers to strings. For
example, to convert 100 into a string :
• 100.to_s
Ruby: Boolean Datatype

• Boolean type variables can hold two


values true or false. But in ruby, it can also
hold third type of value in Boolean variable
called nil. But in most context, we work with
only true and false values. So, let's
concentrate on those two.
Ruby: Constants

• Constants are variables that holds the same


value throughout the program. Ruby is a
typical language, where you can reassign
constants. As per conventions, all constants in
ruby in uppercase characters so that they are
easily distinguishable.
Types of Variables in Ruby
• There are 4 different types of variables
in Ruby. They are :

• Local Variables
• Class Variables
• Instance Variables
• Global Variables
Reserved Words/Keywords in Ruby
• There are certain words that are reserved for
doing specific tasks. These words are known
as keywords and they
have standard, predefined meaning in Ruby.
• These keywords cannot be used for naming
variables or constants in Ruby.
Arrays in Ruby
• An array stores multiple values of the same
data type. Array is similar to range, but the
only difference is that range must represent
continuous sequences.
• Array on the other hand contains collection of
data; it doesn't need to be consecutive
sequence. The real life examples of arrays are:

• Egg cartons containing eggs


• Chess board with its pieces etc
Ruby: Defining an Array

• We define an array by putting square


brackets around data.

• For example,

• numbers = [1,3,5,7,9]Here numbers is a


collection of 5 numbers(data) of type integers.
• We define an array by putting square
brackets around data.

• For example,
• numbers = [1,3,5,7,9]

• Here numbers is a collection


of 5 numbers(data) of type integers.
• The element of an array is accessed by using
array name followed by index. For example,
the first element of numbers array is accessed
by numbers[0]
• o sum up all the elements of the grades array,
following statement is used.

• Sum = grades[0] + grades[1] + grades[2] +


grades[3]
Ruby: What is Hash?

• Hash is another data structure to store the


collection of data. It is often called as associative
structure, because we store data in key-
value pairs.

• A classic example of associative structure


is Dictionary, where the word is the key and the
definition of the word is the value. In hash, to
find the value we have to look at the key.
EXAMPLE
• pincode = {
• 'Abc' = '123',
• 'xyz' = '980',
• 'def' = '456',
• }
Ruby: Representation of Hash

• We retrieve the value from the hash by


presenting the key to the hash name like an
array subscript.
• To retrieve the number of Praveen
: numbers['Praveen']
• If we try to retrieve a value using a key that
doesn't exist, it returns nil.
METHODS
Push and Pop method in Ruby
• Ruby: Push operation
• nother way to add or to push item into an
array is using push() method.
• sample.push("second item")
Ruby: Pop operation

• To remove the element from an array, we


use pop() method.
• sample.pop

You might also like