Ruby program to swap two arrays



Write a Ruby program to swap two arrays

class SwappingArray

  def doSwap
    a = []
    b = []
    c = []
    puts "Enter the first array values "
    (0..4).each do |i|
      a[i] = gets.chomp
    end
    puts "Enter the Second array values "
    (0..4).each do |i|
      b[i] = gets.chomp
    end
    puts "Array before swapping"
    puts "Array first: "+ a.to_s
    puts "Array second: "+ b.to_s
    c = a 
    a = b 
    b = c 
    puts "Array after swapping"
    puts "Array first: "+ a.to_s
    puts "Array second: "+ b.to_s
  end
end

p = SwappingArray.new 

p.doSwap

Output:



Enter the first array values 
 1
 2
 3
 4
 5
Enter the Second array values 
 11
 22
 33
 44
 55
Array before swapping
Array first: ["1", "2", "3", "4", "5"]
Array second: ["11", "22", "33", "44", "55"]
Array after swapping
Array first: ["11", "22", "33", "44", "55"]
Array second: ["1", "2", "3", "4", "5"]


No comments:

Post a Comment