This rule emphasizes the importance of using Kernel#loop with a break statement for post-loop tests in Ruby. The Kernel#loop with break construct is more idiomatic and readable in Ruby than using begin..end while or begin..end until. It also avoids potential confusion about whether the loop will execute at all if the condition is not met initially.
Good readability and clarity are crucial in programming, especially in large codebases where multiple developers are working. It becomes easier for others to understand and maintain the code when it’s written in a more idiomatic way.
To adhere to this rule and improve your coding practices, always use the Kernel#loop with a break statement when you need to perform post-loop tests. This way, you can increase the readability of your code and make it more Ruby-like. For example, instead of writing begin..end while, you can write loop do..break unless..end.
Non-Compliant Code Examples
require_relative'../../spec_helper'require_relative'fixtures/classes'describe"Thread.list"doit"includes the current and main thread"doThread.list.shouldinclude(Thread.current)Thread.list.shouldinclude(Thread.main)endit"includes threads of non-default thread groups"dot=Thread.new{sleep}beginThreadGroup.new.add(t)Thread.list.shouldinclude(t)ensuret.killt.joinendendit"does not include deceased threads"dot=Thread.new{1;}t.joinThread.list.should_notinclude(t)endit"includes waiting threads"doq=Queue.newt=Thread.new{q.pop}beginThread.passwhilet.statusandt.status!='sleep'Thread.list.shouldinclude(t)ensureq<<nilt.joinendendit"returns instances of Thread and not null or nil values"dospawner=Thread.newdoArray.new(100)doThread.new{}endendbeginThread.list.each{|th|th.shouldbe_kind_of(Thread)}endwhilespawner.alive?threads=spawner.valuethreads.each(&:join)endend