I love you ruby, `unless`...

An article, posted 4 months ago filed in sorting, ruby & programming.

I’m quite familiar with writing ruby code. I’ve even expressed my love for the language long time ago. But some constructs still bug me. One of them is unless…

I’ve seen variants of this code:

temperature = 36.7
unless temperature > 37.2 || temperature < 35.5
  puts "Healthy temperature. Temperature is within the normal range."
end
# Output: "Healthy temperature. Temperature is within the normal range."

Just don’t.

I like unless sometimes, but for simple expressions:

print "normal" unless abnormal

But more complex conditionals should be if statements:

if temperature > 35.5 && temperature < 37.2
  puts "Healthy temperature. Temperature is within the normal range."
end

or perhaps even nicer:

if (35.5...37.2).include?
  puts "Healthy temperature. Temperature is within the normal range."
end

Op de hoogte blijven?

Maandelijks maak ik een selectie artikelen en zorg ik voor wat extra context bij de meer technische stukken. Schrijf je hieronder in:

Mailfrequentie = 1x per maand. Je privacy wordt serieus genomen: de mailinglijst bestaat alleen op onze servers.