KEMBAR78
RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING | PPTX
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

RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING

  • 1.
  • 2.
  • 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 ifconditional [then] code... end • Note: The end keyword is required to indicate the end of the if.
  • 5.
    Example 1 To checksome value greater or not a = 42 if a > 7 puts "Yes“ end Output: yes
  • 6.
    Example 2 To checktwo 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 guessthe age age = 15 if age > 18 puts "Welcome“ else puts "Too young“ end Output: Too young
  • 9.
    Example 4 To guessthe 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 guessthe 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
  • 11.
    Ruby if modifier SYNTAX codeif condition • Note: Executes code if the conditional is true.
  • 12.
    Example 6 To guessthe number debug = 1 print "debugn“ if debug Output: debug
  • 13.
    Ruby unless Statement SYNTAX unlessconditional [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 checkthe number greater or lesser a = 42 unless a < 10 puts "Yes“ else puts "No“ end Output: Yes
  • 15.
    Ruby unless modifier SYNTAX codeunless conditional • Note: Executes code if conditional is false.
  • 16.
    Example 8 To checkthe 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 unlessin 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 caseexpression [when expression [, expression ...] [then] code ]... [else code ] end Note that the case expression must be closed with the end keyword.
  • 19.
    Example 10 To guessthe age age = 5 case age when 1, 2, 3 puts "Little baby" when 4, 5 puts "Child“ end Output: Child
  • 20.
    Example 11 To guessthe 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
  • 21.
    LOOPS • While • Until •Ranges • For • Loop break • Loop next • Loop do
  • 22.
    While Statement SYNTAX while conditional[do] code end Note: Executes code while conditional is true.
  • 23.
    Example 12 x =0 while x < 10 puts x x += 1 end 0 1 2 3 4 5 6 7 8 9 CODE OUTPUT
  • 24.
    Until Loops SYNTAX until conditional[do] code end Note: Executes code while conditional is false
  • 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 putsa 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 iin (1..10) puts i end 1 2 3 4 5 6 7 8 9 10 CODE OUTPUT
  • 30.
    Example 16 for iin 1..5 break if i > 3 puts i end 1 2 3 CODE OUTPUT
  • 31.
    Example 17 for iin 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
  • 33.
    ARRAYS • Creation • Accessing •Updating • Adding • Removing • Ranges • Manipulations
  • 34.
    ARRAYS SYNTAX names = [item1,item2…itemn] Example items = ["Apple", "Orange", "Banana"] Note: An Array is essentially a list of numbered items.
  • 35.
    Accessing array elements SYNTAX putsarray_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
  • 37.
    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
  • 38.
    Array Ranges nums =[6, 3, 8, 7, 9] print nums[1..3] [3, 8, 7] CODE OUTPUT
  • 39.
    Array Manipulations Joining twoarrays 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 commonelements 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 toiterate arr = ["a", "b", "c"] for x in arr puts "Value: #{x}“ end Value: a Value: b Value: c CODE OUTPUT
  • 42.
    Hashes Hashes (sometimes knownas 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"]
  • 43.
  • 44.
  • 45.
    Example 19 def say puts"Hi" end say Hi CODE OUTPUT
  • 46.
    Example 20 def sqr(x) putsx*x end sqr(8) 64 CODE OUTPUT
  • 47.
    Example 21 def sum(a,b) puts a+b end sum(7, 4) 11 CODE OUTPUT
  • 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="") ifname=="" puts "Greetings!" else puts "Welcome, #{name}" end end greet(gets.chomp) Welcome, hi CODE OUTPUT
  • 50.
    Example 22 def someMethod(*p) putsp end someMethod(25, "hello", true) 25 hello true CODE OUTPUT
  • 51.
    BLOCKS A block isalways invoked from a function with the same name as that of the block. SYNTAX block_name { statement1 statement2 .......... }
  • 52.
    Example 23 def test yield end test{puts "Hello world"} Hello world 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 yield5 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
  • 55.
    Example 23 def test(&block) block.call end test{ puts "Hello World!"} Hello world CODE OUTPUT