[Ruby] Behavior of evaluation of conditional expression in while

Introduction

I encountered the first behavior while using while, so make a note of it.

environment

ruby: 2.7.1

phenomenon

Phenomenon_A

a = 0

while
  if a == 5
    break
  end
  a += 1
end

puts a
#Execution result: 0

Phenomenon_B

b = 0

while
  b += 1
  if b == 5
    break
  end
end

puts b
#Execution result: 5

Phenomenon_C

c = 0

while c < 5 do
  c += 1
end

puts c
#Execution result: 5

Phenomenon_D

d = 0

loop do
  if d == 5
    break
  end
  d += 1
end

puts d
#Execution result: 5

Phenomenon_E

e = 0

loop do
  e += 1
  if e == 5
    break
  end
end

puts e
#Execution result: 5

As a result I expected, I thought that 5 would output even with phenomenon _A.

Why this happens

(It was solved by @ scivola's comment.)

Apparently, the expression immediately after while is evaluated as a conditional expression. In other words, in this phenomenon_A,

if a == 5
  break
end

This part is evaluated for the first time as a conditional expression of while, and of course ʻa == 5 becomes false and the value of ʻif expression becomes nil, so it seems that it has escaped from the loop.

By the way, in Ruby, all values except nil and false are true.

When I gave a conditional expression as follows, the processing in the loop was executed.

a = 0

while true do #do is optional
  if a == 5
    break
  end
  a += 1
end

puts a
#Execution result: 5

The pitfall of this time was that the expression inside the loop was actually a conditional expression.

Edit history

2020/07/04 17:11 In the comment, @scivola explained the cause of the problem, so I edited "Why this happens". 2020/07/04 17:31 I changed the title to match the content and proofread the text a little.

Remarks

I don't usually use while or loop, but use another iterative method.

Recommended Posts

[Ruby] Behavior of evaluation of conditional expression in while
[Ruby] if statement concept of conditional expression
[Ruby] Conditional branching Conditional expression order
Judgment of fractions in Ruby
About the behavior of ruby Hash # ==
Order of route evaluation in Spark
Basics of sending Gmail in Ruby
Implementation of ls command in Ruby
Acquisition of article information in ruby ​​scraping
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0
Summary of hashes and symbols in Ruby
[Ruby basics] About the role of true and break in the while statement
[Ruby] Classification and usage of loops in Ruby
Unexpected behavior of default message in org.springframework.context.MessageSource.getMessage ()
Ruby conditional branch (case, while, infinite loop, break)
Behavior when wild card (**) is specified in ruby
Recommendation of Service class in Ruby on Rails
[Ruby] Assign variables in conditional expressions in if statements.
Enumerate subsets of arrays given in Ruby (+ α)
Create a native extension of Ruby in Rust
Class in Ruby
Basics of Ruby
Heavy in Ruby! ??
Ruby regular expression
Behavior of ThreadPoolTaskExecutor
Count the number of occurrences of a string in Ruby
[Ruby] How to use standard output in conditional branching
[Ruby] The role of subscripts in learning elements in arrays
Addition of variables in iterative processing in a while statement
Get the URL of the HTTP redirect destination in Ruby
Handling of date and time in Ruby. Use Date and Time properly.
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
[Java] It seems that `0 <hoge <10` cannot be written in the conditional expression of the ʻif` statement.