Ruby always runs while the first time

Asked 2 years ago, Updated 2 years ago, 104 views

while false
  puts1
end

Even under these conditions, ruby does not have a while to run at least once.

ruby-on-rails ruby

2022-09-30 21:28

2 Answers

You can combine the begin clause with the while qualifier.

 begin
  puts1
end while false

However, this notation is not recommended in the Ruby Style Guide. Use the loop/break combination instead.

loop do
  puts1
  breakless false
end

The while qualifier behaves differently depending on whether or not it is combined with the begin clause, so it is a cause of misunderstanding.

 puts1 while false# Do nothing

begin;puts1;end while false#puts1 is called


2022-09-30 21:28

 begin
  puts1
end while false

How about


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.