jeudi 22 février 2018

Little bit confusing about the array and loops in rails, so plz explain clearly

@service_center ||= []
@code2 ||= [] These two variables are declared as an array.

I have an Service_center array.

service_center[] = [<['first_array1','first_array2']>, <['second_arr1','second_arr2']

I want to Print all the elements from the array. So i am using while loop and for loop the code is here.

j=0
while !@service_center[j].nil? 
    k=0       
    @service_center[j].each do |a|
        @code2[k]= a
        k += 1
    end
j += 1
end 

so @code2 variable have [<['first_array1','first_array2']>, <['second_arr1','second_arr2'] but i print the variable it displays the second array ['second_arr1','second_arr2'].why i cant get both the array.

Aucun commentaire:

Enregistrer un commentaire