3. CONDITION
• If statement
• If modifier
• Multiple if
• If else
• If else modifier
• Unless if
• Unless if modifier
• Case statement
4. Ruby if… Statement
SYNTAX
if conditional [then]
code...
end
• Note: The end keyword is required to indicate the
end of the if.
5. Example 1
To check some value greater or not
a = 42
if a > 7
puts "Yes“
end
Output:
yes
6. 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
7. 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.
8. Example 3
To guess the age
age = 15
if age > 18
puts "Welcome“
else
puts "Too young“
end
Output:
Too young
9. 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
10. 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
12. Example 6
To guess the number
debug = 1
print "debugn“ if debug
Output:
debug
13. 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.
14. Example 7
To check the number greater or lesser
a = 42
unless a < 10
puts "Yes“
else
puts "No“
end
Output:
Yes
16. 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
17. 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
18. 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.
19. 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
20. 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
35. 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.
36. 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
39. 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
40. 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
41. For loop to iterate
arr = ["a", "b", "c"]
for x in arr
puts "Value: #{x}“
end
Value: a
Value: b
Value: c
CODE OUTPUT
42. 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"]
48. 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
49. Example 21
def greet(name="")
if name==""
puts "Greetings!"
else
puts "Welcome,
#{name}"
end
end
greet(gets.chomp)
Welcome, hi
CODE OUTPUT
53. 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
54. 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