ruby - Iterating over every name in a nested hash in array -
i have:
students = [ {name: "dr. hannibal lecter", cohort: :november}, {name: "darth vader", cohort: :november}, {name: "nurse ratched", cohort: :november}, {name: "michael corleone", cohort: :november}, {name: "alex delarge", cohort: :november}, {name: "the wicked witch of west", cohort: :november}, {name: "terminator", cohort: :november}, {name: "freddy krueger", cohort: :november}, {name: "the joker", cohort: :november}, {name: "joffrey baratheon", cohort: :november}, {name: "norman bates", cohort: :november} ]
i attempted iterate on every student's name in loop:
def print_loop(students) puts "would list students? 'y/n'" answer = gets.chomp while answer != "n" = students[-1] students.each |student, value| puts student[:name] break if if answer == "y" exit end end end end
it iterates , loops on first name. how iterate on every name? doing wrong?
if have use while loop, can use incrementing counter , loop while counter less size of array
i = 0 while < students.size puts students[i][:name] += 1 end
note idiosyncratic in ruby , use #each
loop.
if want use while loop allow user request continue after every name...
i = 0 puts "would list students? 'y/n'" answer = gets.chomp while answer == 'y' puts students[i][:name] += 1 puts "keep going? 'y/n'" answer = gets.chomp end
Comments
Post a Comment