Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Ruby and Rails

Array Operations in Ruby

Book navigation

  • Single line swapping in Ruby
  • Array Operations in Ruby
  • File operations in Ruby
  • User defined functions in Ruby
  • Classes and Objects in Ruby
  • Simple Form Hello Rails Application
  • Database Application using scaffold - [Video]
  • Session Management
  • Cookies Management in Ruby on Rails
  • File Uploads in Ruby on Rails
By Karthikeyan , 22 October, 2012
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
  • Add new comment

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy