unless

unless

unless는 조건이 거짓 값(falsey) 이면 then 분기를, else 분기가 있다면 그 외의 경우에 else 분기를 실행해요. 즉 if와 반대 방향으로 동작한다고 보면 됩니다.

unless some_condition
  expression_when_falsey
else
  expression_when_truthy
end

# The above is the same as:
if some_condition
  expression_when_truthy
else
  expression_when_falsey
end

# Can also be written as a suffix
close_door unless door_closed?

코드에 "~이 아니라면"이라는 흐름이 자연스러울 때 if보다 읽기 좋은 선택이에요. 위 예시처럼 접미사(suffix) 형태로 짧게 쓰는 것도 가능합니다. 다만 조건이 복잡해지면 오히려 헷갈릴 수 있으니, 상황에 맞게 골라 쓰는 게 좋아요.

출처: Crystal 공식 문서 - unless

더 알아보기 (Learn more)