Reason #153 • June 2nd, 2026

One-liner while / until loops

In Ruby, you can write while and until loops on a single line by placing the condition at the end of the statement:

Ruby
integer = 0

puts integer += 1 while integer < 5
# Output:
# 1
# 2
# 3
# 4
# 5

puts integer -= 1 until integer.zero?
# Output:
# 4
# 3
# 2
# 1
# 0
    

Mind-boggling yet obvious in the best of ways, wouldn't you say?

That said, it is quite rare to get the opportunity to use this syntax in practice. However, what inspired me to start writing about this was a thought I had while writing the article on Using blocks for connection pooling - that a #shutdown method on the pool we implemented there provided one of those opportunities:

Ruby
class HTTPPool
  # ...

  def shutdown
    @connections.pop.close until @connections.empty?
  end
end
    

So very satisfying! 😌