Replace one array with another : while « Statement « Ruby






Replace one array with another


#!/usr/bin/env ruby

i = 0
breeds = [ "quarter", "arabian", "appalosa", "paint" ]
puts breeds.size # => 4
temp = []

while i < breeds.size do
  temp << breeds[i].capitalize
  i += 1
end

temp.sort! # => ["Appalosa", "Arabian", "Paint", "Quarter"]
breeds.replace( temp )
p breeds # => ["Appalosa", "Arabian", "Paint", "Quarter"]

 








Related examples in the same category

1.Here's the formal specification for the while loop:
2.The while Loop
3.Another form you can use is with begin/end
4.The while loop executes its contained code while a condition that you specify remains true.
5.While loop with array length
6.loop code based on the result of a comparison made on each loop
7.use while loop to convert elements in an array to capitalized case