Ruby/Statement/break
Материал из Wiki.crossplatform.ru
(Различия между версиями)
ViGOur (Обсуждение | вклад) м (1 версия: Импорт выборки материалов по Ruby) |
Текущая версия на 18:00, 13 сентября 2010
Содержание |
break ends the immediately enclosing loop.
loop do puts "Running..." print "Enter q to quit: " gets chomp break if $_ == "q" end
Break if
block = Proc.new do |x| puts x break if x == 3 puts x + 2 end block.call(5) # 5 # 7 block.call(3) # 3 # LocalJumpError: break from proc-closure
Break out of a block
def oscillator x = 1 while true yield x x *= -2 end end oscillator { |x| puts x; break if x.abs > 50; }
Read till "quit"
while(line = gets.chop) break if line == "quit" puts eval(line) end puts "Good bye"
Stopping an Iteration
1.upto(10) do |x| puts x break if x == 3 end # 1 # 2 # 3