require"rails"classTourincludeRails::Initializableattr_reader:citiesdefinitialize@cities=[]endend###一开始的计划是:先去USA,再去Britain,最后去France,Tour.initializer("USA")do|do_the_same_th|@cities<<{country:"USA",city:"Silicon Valley",do:do_the_some_th}endTour.initializer("France",before:"Britain")do|do_the_same_th|@cities<<{country:"France",city:"Paris",do:do_the_same_th}endTour.initializer("Britain",before:"USA")do|do_the_same_th|@cities<<{country:"Britain",city:"London",do:do_the_same_th}end###后来我改变注意了,想去英国后,先回趟中国,再去法国Tour.initializer("China",before:"Britain",after:"France")do|do_the_same_th|@cities<<{country:"China",city:"Beijin",do:do_the_same_th}endmy_tour=Tour.newmy_tour.run_initializers(:default,"take a photo")my_tour.cities.each_with_indexdo|tour,index|p"#{index}: Went to #{tour[:country]}-#{tour[:city]} and #{tour[:do_the_same_th]}"end
最后的输出结果如下:
"0: Went to USA-Silicon Valley and "
"1: Went to Britain-London and "
"2: Went to China-Beijin and "
"3: Went to France-Paris and "