SlideShare a Scribd company logo
RUBY PROGRAMMING
Content
• CONDITION
• ARRAYS
• HASHES
• BLOCKS
CONDITION
• If statement
• If modifier
• Multiple if
• If else
• If else modifier
• Unless if
• Unless if modifier
• Case statement
Ruby if… Statement
SYNTAX
if conditional [then]
code...
end
• Note: The end keyword is required to indicate the
end of the if.
Example 1
To check some value greater or not
a = 42
if a > 7
puts "Yes“
end
Output:
yes
Example 2
To check two conditions
num = 16
if num > 7
puts "Greater than 7“
if num < 42
puts "Between 7 and
42“
end
end
Output:
Greater than 7
Between 7 and 42
Ruby if else… Statement
SYNTAX
if conditional [then]
code...
[elsif conditional [then]
code...]...
[else
code...]
end
• Note: The end keyword is only needed for the if statement, as
the else block is part of the if expression.
Example 3
To guess the age
age = 15
if age > 18
puts "Welcome“
else
puts "Too young“
end
Output:
Too young
Example 4
To guess the number greater than 2 or not
x = 1
if x > 2
puts "x is greater than 2"
elsif x < 2 and x!=0
puts "x is 1"
else
puts "I can't guess the
number"
end
Output:
x is 1
Example 5
To guess the number
num = 8
if num == 3
puts "Number is 3“
elsif num == 10
puts "Number is 10“
elsif num == 7
puts "Number is 7“
else
puts "Not found“
end
Output:
Not found
Ruby if modifier
SYNTAX
code if condition
• Note: Executes code if the conditional is true.
Example 6
To guess the number
debug = 1
print "debugn“ if debug
Output:
debug
Ruby unless Statement
SYNTAX
unless conditional [then]
code
[else
code ]
end
• Note: The unless expression is the opposite of an if expression. It
executes code when a conditional is false.
Example 7
To check the number greater or lesser
a = 42
unless a < 10
puts "Yes“
else
puts "No“
end
Output:
Yes
Ruby unless modifier
SYNTAX
code unless conditional
• Note: Executes code if conditional is false.
Example 8
To check the number greater or lesser
using unless
a = 42
puts "Yes" if a > 10
puts "Yes" unless a < 10
Output:
Yes
Yes
Example 9
Using unless in multiple if
var = 1
print "1 -- Value is setn" if var
print "2 -- Value is setn" unless var
var = false
print "3 -- Value is setn" unless var
Output:
1 -- Value is set
3 -- Value is set
Ruby case Statement
SYNTAX
case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end
Note that the case expression must be closed with the end keyword.
Example 10
To guess the age
age = 5
case age
when 1, 2, 3
puts "Little baby"
when 4, 5
puts "Child“
end
Output:
Child
Example 11
To guess the age using else
age = 18
case age
when 1, 2, 3
puts "Little baby"
when 4, 5
puts "Child"
else
puts “Adult“
end
Output:
Adult
LOOPS
• While
• Until
• Ranges
• For
• Loop break
• Loop next
• Loop do
While Statement
SYNTAX
while conditional [do]
code
end
Note: Executes code while conditional is true.
Example 12
x = 0
while x < 10
puts x
x += 1
end
0 1 2 3 4 5 6 7 8 9
CODE OUTPUT
Until Loops
SYNTAX
until conditional [do]
code
end
Note: Executes code while conditional is false
Example 13
a = 0
until a > 10
puts "a = #{a}"
a +=2
end
a = 0
a = 2
a = 4
a = 6
a = 8
a = 10
CODE OUTPUT
Ranges
a = (1..7).to_a
puts a
b = (79...82).to_a
puts b
c = ("a".."d").to_a
puts c
[1, 2, 3, 4, 5, 6, 7]
[79, 80, 81]
[a, b, c, d]
CODE OUTPUT
Example 14
age = 42
case age
when 0..14
puts "Child“
when 15..24
puts "Youth“
when 25..64
puts "Adult“
Else
puts "Senior“
end
Adult
CODE OUTPUT
For Loop
SYNTAX
for variable [, variable ...] in expression [do]
code
end
• Note: Executes code once for each element
in expression.
Example 15
for i in (1..10)
puts i
end
1 2 3 4 5 6 7 8 9 10
CODE OUTPUT
Example 16
for i in 1..5
break if i > 3
puts i
end
1
2
3
CODE OUTPUT
Example 17
for i in 0..10
next if i %2 == 0
puts i
End
Note: Next statement skip one
iteration
1 3 5 7 9
CODE OUTPUT
Example 18
x = 0
loop do
puts x
x += 1
break if x > 10
end
0 1 2 3 4 5 6 7 8 9 10
CODE OUTPUT
ARRAYS
• Creation
• Accessing
• Updating
• Adding
• Removing
• Ranges
• Manipulations
ARRAYS
SYNTAX
names = [item1,item2…item n]
Example
items = ["Apple", "Orange", "Banana"]
Note:
An Array is essentially a list of numbered items.
Accessing array elements
SYNTAX
puts array_name[index_value]
Example
puts items[0]
Note:
A negative index is assumed relative to the end of
the array. For example, an index of -1 indicates
the last element of the array.
Adding Elements
arr = [5, "Dave", 15.88, false]
puts arr[0]
puts arr[1]
puts arr[-1]
arr << 8
puts arr
arr.insert(2, 8)
5
Dave
false
5 Dave 15.88 false 8
5 Dave 8 15.88 false
CODE OUTPUT
Removing Elements
arr = [1, 2, 3]
arr.pop
print arr
arr = [2, 4, 6, 8]
arr.delete_at(2)
print arr
[1, 2]
[2, 4, 8]
CODE OUTPUT
Array Ranges
nums = [6, 3, 8, 7, 9]
print nums[1..3]
[3, 8, 7]
CODE OUTPUT
Array Manipulations
Joining two arrays
a = [1, 2, 3]
b = [4, 5]
res = a + b
print res
Removing elements present in both array
a = [1, 2, 3, 4, 5]
b = [2, 4, 5, 6]
res = a – b
print res
[1, 2, 3, 4, 5]
[1, 3]
CODE OUTPUT
Array Manipulations
Return common elements
a = [2, 3, 7, 8]
b = [2, 7, 9]
print a & b
Join array and remove
duplicates
a = [2, 3, 7, 8]
b = [2, 7, 9]
print a | b
[2, 7]
[2, 3, 7, 8, 9]
CODE OUTPUT
For loop to iterate
arr = ["a", "b", "c"]
for x in arr
puts "Value: #{x}“
end
Value: a
Value: b
Value: c
CODE OUTPUT
Hashes
Hashes (sometimes known as associative
arrays, maps, or dictionaries) are similar to
arrays in that they are an indexed collection
of elements.
Example:
ages = { "David" => 28, "Amy"=> 19, "Rob" => 42 }
puts ages["Amy"]
Examples
h = {"name"=>"Dave",
"age"=>28,
"gender"=>"male"}
puts h["age"]
28
CODE OUTPUT
Methods
SYNTAX
def method_name
code
end
Example 19
def say
puts "Hi"
end
say
Hi
CODE OUTPUT
Example 20
def sqr(x)
puts x*x
end
sqr(8)
64
CODE OUTPUT
Example 21
def sum(a, b)
puts a+b
end
sum(7, 4)
11
CODE OUTPUT
Example 22
def sum(a, b=8)
puts a+b
end
x = 5
sum(x)
Note: You can also set default
values for the parameters, so
that the method will still work
even if you do not provide all
the arguments.
13
CODE OUTPUT
Example 21
def greet(name="")
if name==""
puts "Greetings!"
else
puts "Welcome,
#{name}"
end
end
greet(gets.chomp)
Welcome, hi
CODE OUTPUT
Example 22
def someMethod(*p)
puts p
end
someMethod(25, "hello", true)
25 hello true
CODE OUTPUT
BLOCKS
A block is always invoked from a function with the
same name as that of the block.
SYNTAX
block_name
{
statement1
statement2
..........
}
Example 23
def test
yield
end
test{ puts "Hello world"}
Hello world
CODE OUTPUT
Example 24
def test
puts "You are in the method"
yield
puts "You are again back to
the method"
yield
end
test {puts "You are in the
block"}
You are in the method
You are in the block
You are again back to the
method
You are in the block
CODE OUTPUT
Example 25
def test
yield 5
puts "You are in the method
test"
yield 100
end
test {|i| puts "You are in the
block #{i}"}
You are in the block 5
You are in the method test
You are in the block 100
CODE OUTPUT
Example 23
def test(&block)
block.call
end
test { puts "Hello World!"}
Hello world
CODE OUTPUT

More Related Content

PDF
Cs psuedocode-basics-v4
PDF
The Ring programming language version 1.3 book - Part 12 of 88
PPT
python fundamental for beginner course .ppt
PPTX
Bikalpa_Thapa_Python_Programming_(Basics).pptx
PPTX
Python
PDF
The Ring programming language version 1.5.4 book - Part 20 of 185
PDF
The Ring programming language version 1.7 book - Part 23 of 196
PPTX
Intro to ruby
Cs psuedocode-basics-v4
The Ring programming language version 1.3 book - Part 12 of 88
python fundamental for beginner course .ppt
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Python
The Ring programming language version 1.5.4 book - Part 20 of 185
The Ring programming language version 1.7 book - Part 23 of 196
Intro to ruby

Similar to RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING (20)

PPTX
Algebra 1 chapter 1 complete notes
PDF
The Ring programming language version 1.5.2 book - Part 19 of 181
PPTX
Ruby Basics
PPT
C tutorial
DOC
Advanced REXX Programming Techniques
DOCX
Pseudo code practice problems+ c basics
PDF
python notes.pdf
PDF
python 34💭.pdf
PDF
pythonQuick.pdf
PPT
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
PDF
Saigon Ruby Meetup 06/10/2015 - 5 Random Ruby Tips
PPTX
Ruby introduction part1
PDF
algorithm
PDF
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
PDF
Reason - introduction to language and its ecosystem | Łukasz Strączyński
PDF
Ruby Topic Maps Tutorial (2007-10-10)
PPTX
Python programing
PDF
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
PPTX
Looping statement in python
PDF
The Ring programming language version 1.9 book - Part 26 of 210
Algebra 1 chapter 1 complete notes
The Ring programming language version 1.5.2 book - Part 19 of 181
Ruby Basics
C tutorial
Advanced REXX Programming Techniques
Pseudo code practice problems+ c basics
python notes.pdf
python 34💭.pdf
pythonQuick.pdf
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Saigon Ruby Meetup 06/10/2015 - 5 Random Ruby Tips
Ruby introduction part1
algorithm
‘How to develop Pythonic coding rather than Python coding – Logic Perspective’
Reason - introduction to language and its ecosystem | Łukasz Strączyński
Ruby Topic Maps Tutorial (2007-10-10)
Python programing
/Regex makes me want to (weep_give up_(╯°□°)╯︵ ┻━┻)/i (for 2024 CascadiaPHP)
Looping statement in python
The Ring programming language version 1.9 book - Part 26 of 210
Ad

Recently uploaded (20)

PPTX
Glazing at Facade, functions, types of glazing
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Top 10 read articles In Managing Information Technology.pdf
PPTX
“Next-Gen AI: Trends Reshaping Our World”
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
web development for engineering and engineering
PPTX
TE-AI-Unit VI notes using planning model
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PDF
International Journal of Information Technology Convergence and Services (IJI...
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Glazing at Facade, functions, types of glazing
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Top 10 read articles In Managing Information Technology.pdf
“Next-Gen AI: Trends Reshaping Our World”
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Road Safety tips for School Kids by a k maurya.pptx
Simulation of electric circuit laws using tinkercad.pptx
ETO & MEO Certificate of Competency Questions and Answers
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
436813905-LNG-Process-Overview-Short.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
web development for engineering and engineering
TE-AI-Unit VI notes using planning model
anatomy of limbus and anterior chamber .pptx
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
International Journal of Information Technology Convergence and Services (IJI...
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Ad

RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING