Julian A Avar C
Julian A Avar C

Reputation: 878

ruby: difference between (0..) and (0...)

As far as I can tell (0..) and (0...) do and are the same thing, however if you look at the range documentation it says that

  • (1..) and (1...) are not equal, although technically representing the same sequence.

So I kept looking everywhere and I couldn't find what the difference is.

What differentiates the two ranges above?

Upvotes: 1

Views: 65

Answers (1)

rmlockerd
rmlockerd

Reputation: 4126

From RubyDocs:

rng == obj → true or false

Returns true only if obj is a Range, has equivalent begin and end items (by comparing them with ==), and has the same exclude_end? setting as the range.

So:

(1..).exclude_end?
=> false

(1...).exclude_end?
=> true

(1..) == (1...)
=> false

So, a practically meaningless distinction, as @max says, but technically Ruby considers them to be different since one is technically infinity - 1.

Upvotes: 4

Related Questions