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
25. 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
26. 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
27. 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
28. For Loop
SYNTAX
for variable [, variable ...] in expression [do]
code
end
âą Note: Executes code once for each element
in expression.
29. Example 15
for i in (1..10)
puts i
end
1 2 3 4 5 6 7 8 9 10
CODE OUTPUT
30. Example 16
for i in 1..5
break if i > 3
puts i
end
1
2
3
CODE OUTPUT
31. 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
32. 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
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