Ruby/Statement/catch
Материал из Wiki.crossplatform.ru
(Различия между версиями)
ViGOur (Обсуждение | вклад) м (1 версия: Импорт выборки материалов по Ruby) |
Текущая версия на 18:00, 13 сентября 2010
Catch and Throw
catch(:finish) do 1000.times do x = rand(1000) throw :finish if x == 123 end puts "Generated 1000 random numbers without generating 123!" end
catch and throw don"t have to be directly in the same scope.
# throw works from methods called from within a catch block: def generate_random_number_except_123 x = rand(1000) throw :finish if x == 123 end catch(:finish) do 1000.times { generate_random_number_except_123 } puts "Generated 1000 random numbers without generating 123!" end