Basic Array Operations in Ruby
a = [4,3,7,5,2,7]
a1=['Mon','Tue','Wed','Thu','May']
a2=['Jan','Feb','Mar','Apr','May']
puts "Array Operations"
puts "Arry A : #{a}"
puts "Arry A1 : #{a1}"
puts "Arry A2 : #{a2}"
puts "1.1. Multiply with Int (A1*2): #{a1*2} "
puts "1.2. Multiply with String (A*h)#{a*"h"}"
puts "2.Concatenation(+) : A1 + A2 :#{a1+a2} "
puts "3.Append : A1 << A2 : #{a1<<a2} "
#Array appended so we have to reset a1
a1=['Mon','Tue','Wed','Thu','May']
puts "4.Union : A1 | A2 : #{a1|a2} "
puts "5. Intersection (A1&A2) :#{a1&a2}"
puts "6.1 Difference A1 - A2 : #{a1-a2}"
puts "6.2 Difference A2 - A1 : #{a2-a1}"
puts "7. Comparison (A1==A2) : #{a1==a2}"
puts "8. Transforming (A.map { |i| i + 1 }) : #{a.map { |i| i + 1 }}"
# select even numbers
puts "9. Filtering A.select {|number| number % 2 == 0} : #{a.select {|number| number % 2 == 0}}"
puts "10. Deleting (A.delete_if{|i| i < 5 }) : #{a.delete_if{|i| i < 5 }}"
Output
Array Operations
Arry A : 437527
Arry A1 : MonTueWedThuMay
Arry A2 : JanFebMarAprMay
1.1. Multiply with Int (A1*2): MonTueWedThuMayMonTueWedThuMay
1.2. Multiply with String (A*h)4h3h7h5h2h7
2.Concatenation(+) : A1 + A2 :MonTueWedThuMayJanFebMarAprMay
3.Append : A1 << A2 : MonTueWedThuMayJanFebMarAprMay
4.Union : A1 | A2 : MonTueWedThuMayJanFebMarApr
5. Intersection (A1&A2) :May
6.1 Difference A1 - A2 : MonTueWedThu
6.2 Difference A2 - A1 : JanFebMarApr
7. Comparison (A1==A2) : false
8. Transforming (A.map { |i| i + 1 }) : 548638
9. Filtering A.select {|number| number % 2 == 0} : 42
10. Deleting (A.delete_if{|i| i < 5 }) : 757