Project

General

Profile

Actions

Bug #13802

closed

break inside loop is not working as expected

Added by lingarajg (Lingaraj Gowdar) over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
[ruby-core:82335]

Description

Reproduce process:

  1. Create a file (.rb)
  2. Add the below code
  3. Run with syntax - ruby .rb

your ruby version (ruby -v):
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

reproducible ruby script:

y=1

loop do
	y += 1
	next unless y%2==0
	puts y
	break if y > 10
end

Result of reproduce process:

2
4
6
8
10
12
[Finished in 0.0s]

Expected result and the reason why you expect:

2
4
6
8
10
[Finished in 0.0s]

Reason is when the condition "y > 10" is met, the code should exit from there, tested with both "break" and "exit". result is same.


Files

bug.rb (69 Bytes) bug.rb This file contains script which produces unexpected result as per description lingarajg (Lingaraj Gowdar), 08/10/2017 08:34 PM

Updated by MSP-Greg (Greg L) over 6 years ago

The code is working as expected.

There are many ways to provide your preferred output. One way is to place the break statement before the puts statement.

Updated by shevegen (Robert A. Heiler) over 6 years ago

Yes, works as the code specifies, not a bug, so misfiled.

I anyone wants to test via copy/paste into IRB, here is a slightly reformatted variant:

y = 1

loop do
  y += 1
  next unless y % 2==0
  puts y
  break if y > 10
end
Actions #3

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0