# Quick range question

**URL:** <https://forum.crystal-lang.org/t/quick-range-question/3661>\
**Category:** Help & Support\
**Created:** [August 27, 2021, 5:07am UTC](https://forum.crystal-lang.org/t/quick-range-question/3661 "2021-08-27T05:07:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [August 27, 2021, 5:07am UTC](https://forum.crystal-lang.org/t/quick-range-question/3661/1 "2021-08-27T05:07:14Z")

</div>

Do all these compile to the same code, and if not, which one is faster?

```auto
if (x..y).includes? a

if (x..y).covers? a

if x <= a <= y

```

---

<div class="post-metadata">

**Author:** ![kim](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kim/32/693_2.png) [@kim](https://forum.crystal-lang.org/u/kim)\
**Post date:** [August 27, 2021, 5:17am UTC](https://forum.crystal-lang.org/t/quick-range-question/3661/2 "2021-08-27T05:17:03Z")

</div>

They’re the same.

`#includes?` is one less call, but this is likely optimised out during a build. It’s one of the few (/only?) methods in stdlib with dual names.

> <https://github.com/crystal-lang/crystal/blob/6d9a1d5830db5f276bf3df37fceeb0d5333e7e14/src/range.cr#L304-L307>

---

<div class="post-metadata">

**Author:** ![vlazar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vlazar/32/31_2.png) [@vlazar](https://forum.crystal-lang.org/u/vlazar)\
**Post date:** [August 27, 2021, 7:46am UTC](https://forum.crystal-lang.org/t/quick-range-question/3661/3 "2021-08-27T07:46:29Z")

</div>

Interesting.

I wonder if this `covers?` “alias” was introduced before “we discourage aliases in Crystal” altitude?

---

<div class="post-metadata">

**Author:** ![vlazar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vlazar/32/31_2.png) [@vlazar](https://forum.crystal-lang.org/u/vlazar)\
**Post date:** [August 27, 2021, 7:58am UTC](https://forum.crystal-lang.org/t/quick-range-question/3661/4 "2021-08-27T07:58:28Z")

</div>

I’ve tried these in [Compiler Explorer](https://godbolt.org/) where Crystal support was added by `@HertzDevil` recently.

Add `--release` flag in Compiler Options:

```auto
(1..3).includes? 2

(1..3).covers? 2

1 <= 2 <= 3

```

And they seem to produce exactly the same code.

Without `--release` flag they generate different code though, so `covers?` would be 1 more method call and plain conditions would be no method calls at all.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [August 27, 2021, 2:04pm UTC](https://forum.crystal-lang.org/t/quick-range-question/3661/5 "2021-08-27T14:04:44Z")

</div>

Thanks all. In Ruby `cover?` and `include?` are a little bit different.
