# Kernel.=== in case statements, even though I never called it

**URL:** <https://www.ruby-forum.com/t/kernel-in-case-statements-even-though-i-never-called-it/67534>\
**Category:** Ruby\
**Created:** [September 5, 2006, 2:18am UTC](https://www.ruby-forum.com/t/kernel-in-case-statements-even-though-i-never-called-it/67534 "2006-09-05T02:18:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weirdbro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c37758/32.png) [@weirdbro](https://www.ruby-forum.com/u/weirdbro)\
**Post date:** [September 5, 2006, 2:18am UTC](https://www.ruby-forum.com/t/kernel-in-case-statements-even-though-i-never-called-it/67534/1 "2006-09-05T02:18:24Z")

</div>

I was profiling a script I wrote, and the method with the greatest  
amount of time used was Kernel.=== . I was surprised, not only since I  
didn’t use this method in my code, but my code had nothing involving  
modules. When I changed a case statement that was inside my loop to an  
if and many elsifs, I saved about 2 seconds on profiling, and the call  
was gone. Is there a reason such a slow call is used in case statements?

---

<div class="post-metadata">

**Author:** ![weirdbro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c37758/32.png) [@weirdbro](https://www.ruby-forum.com/u/weirdbro)\
**Post date:** [September 5, 2006, 2:28am UTC](https://www.ruby-forum.com/t/kernel-in-case-statements-even-though-i-never-called-it/67534/2 "2006-09-05T02:28:19Z")

</div>

Tomasz W. wrote:

> case uses === (pattern match) not ==.
> 
> It is very useful:
> 
> case str  
> when /foo/  
> …  
> end
> 
> case num  
> when 3…5  
> …  
> end

Oh, I’m quite aware of that. Oh… its calling the objects inherited  
Kernel.===, not calling Kernel::===. Ok, well, in that case, a good  
question would be “Why is it so much slower in simple cases than == is,  
even though for string they do the exact same thing?”

---

<div class="post-metadata">

**Author:** ![weirdbro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c37758/32.png) [@weirdbro](https://www.ruby-forum.com/u/weirdbro)\
**Post date:** [September 5, 2006, 3:22am UTC](https://www.ruby-forum.com/t/kernel-in-case-statements-even-though-i-never-called-it/67534/3 "2006-09-05T03:22:45Z")

</div>

On 9/5/06, Jesse S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Oh, I’m quite aware of that. Oh… its calling the objects inherited  
> Kernel.===, not calling Kernel::===. Ok, well, in that case, a good  
> question would be “Why is it so much slower in simple cases than == is,  
> even though for string they do the exact same thing?”

Kernel#=== calls == method, not aliases it. So you have 2 method calls,  
and two dynamic dispatches instead of one: Kernel#===, then String#==.

You can test this by alias\_method’ing === to == in String class 😉

Relevant code:  
VALUE  
rb\_equal(VALUE obj1, VALUE obj2)  
{  
VALUE result;

```
if (obj1 == obj2) return Qtrue;
result = rb_funcall(obj1, id_eq, 1, obj2);
if (RTEST(result)) return Qtrue;
return Qfalse;

```

}

---

<div class="post-metadata">

**Author:** ![weirdbro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/c37758/32.png) [@weirdbro](https://www.ruby-forum.com/u/weirdbro)\
**Post date:** [September 5, 2006, 2:24am UTC](https://www.ruby-forum.com/t/kernel-in-case-statements-even-though-i-never-called-it/67534/4 "2006-09-05T02:24:41Z")

</div>

On 9/5/06, Jesse S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was profiling a script I wrote, and the method with the greatest  
> amount of time used was Kernel.=== . I was surprised, not only since I  
> didn’t use this method in my code, but my code had nothing involving  
> modules. When I changed a case statement that was inside my loop to an  
> if and many elsifs, I saved about 2 seconds on profiling, and the call  
> was gone. Is there a reason such a slow call is used in case statements?

case uses === (pattern match) not ==.

It is very useful:

case str  
when /foo/  
…  
end

case num  
when 3…5  
…  
end
