Identify the Array and Hash of Customers : Array Each « Array « Ruby






Identify the Array and Hash of Customers




customer_array = ['A','B','C','D','E','F']
customer_hash = {
'A' => 'Fiction',
'B' => 'Mystery',
'C' => 'Computer',
'D' => 'Fiction',
'E' => 'Sports',
'F' => 'Fiction'

}

j=0
for i in 0...customer_array.length
     if customer_hash[customer_array[i]]=='Fiction'
            puts "#{customer_array[i]} has brought fiction books"
            customer_array[j]=customer_array[i]
            j=j+1
     end
end

 








Related examples in the same category

1.If you have a collection, such as an array or a range, you can also use the Ruby each iterator to create a loop.
2.each method goes through each element of the array and passes it as a parameter to the code block you supply.