Latest Changes of this blog, and Ruby's `unless`


I forgot to write the latest changes in this blog.

Changes are:

  • feed URL: now
  • Old Post Path
  • anything else…?

If you registered my blog’s feed in the past, I’m sure it has been broken.

Please change the above URL if you’d be a reader of this blog.

Ruby’s unless

This morning, I tooted about Ruby’s unless use case.

It says, “Do not put multiple conditions after unless in Ruby.”

Some responded to this toot in the local timeline, and I got several good feedbacks.

# Using `unless` with multiple conditions.
# Confusing...
publish! unless state == "published" && published_at.present?

# Using `if`.
# Better
publish! if state != "published" || published_at.blank?

# Define a new private method for the purpose.
# Good
publish! if ready_to_publish?

## Other examples ##

# omg...
x unless x > 1 && x < 10

# ...
x if x < 1 || x > 10

# better?
# i'm ok using `unless` toward single condition.
x unless x.between?(1, 10)
x unless (1..10).cover?(x)

Thought rubocop has a rule like AvoidMultipleConditionsAfterUnless, it doesn’t.

I hate the gem much more.

I don’t understand the pros with using unless for multiple conditions and I asked it to the code-reviewee, but I don’t receive any reply.

I’ll add things on this post after I could get any response from him.