Hacker News new | ask | show | jobs
by brudgers 2752 days ago
Lately, I am enamored of Ruby

  x.between?(6,9)
or very explicitly

  x.between?(5.0.next_float, 10.0.prev_float)
2 comments

But you have to know if the boundaries are inclusive or exclusive, which leaves you with four different possibilities. You and everyone who reads your code needs to know this.

Ruby is my main language and I would have to look up the boundaries of this function.

Your "explicit" statement is also so much worse than using `x < 5.0` or `x <= 5.0`, depending on what you want. This is terrible advice imho.

I am lately enamored of Ruby because it helps me write code that makes me happy. Calling methods on numbers is one of those things that does.

Anyway, ranges are another way of expressing the concepts in Ruby:

  (5.0.next_float..10.0.prev_float).include?(x)
or

  (5.0.next_float...10).include?(x)
if I don't mean

  (5..10).include?(x)
None of which is advice. Just remarks about why I am enamored of Ruby, lately. If I wanted to write C, I'd use Go.
Ruby usually has the most expressive way to write anything!

(and 7 other ways)