# Complie-time polymorphsim and Run-time polymorpshism

**URL:** <https://www.ruby-forum.com/t/complie-time-polymorphsim-and-run-time-polymorpshism/191256>\
**Category:** Ruby\
**Created:** [July 8, 2010, 12:56pm UTC](https://www.ruby-forum.com/t/complie-time-polymorphsim-and-run-time-polymorpshism/191256 "2010-07-08T12:56:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hari\_bhat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/82dd89/32.png) [@hari\_bhat](https://www.ruby-forum.com/u/hari_bhat)\
**Post date:** [July 8, 2010, 12:56pm UTC](https://www.ruby-forum.com/t/complie-time-polymorphsim-and-run-time-polymorpshism/191256/1 "2010-07-08T12:56:26Z")

</div>

Hi

```
                I wanted to know about complie-time polymorphsim

```

and run-time polymorpshism in ruby

with examples.

Regards,  
Hari

---

<div class="post-metadata">

**Author:** ![hari\_bhat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/82dd89/32.png) [@hari\_bhat](https://www.ruby-forum.com/u/hari_bhat)\
**Post date:** [July 8, 2010, 1:40pm UTC](https://www.ruby-forum.com/t/complie-time-polymorphsim-and-run-time-polymorpshism/191256/2 "2010-07-08T13:40:56Z")

</div>

On Thu, Jul 8, 2010 at 12:55 PM, hari bhat [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> Â Â Â Â Â Â Â Â Â Â I wanted to know about complie-time polymorphsim  
> and run-time polymorpshism in ruby

As a rule of thumb Ruby is runtime-only. When you evaluate

```
obj.method

```

obj’s class is checked at that exact point in the execution of the  
program. If you later execute that line again, it is checked again.  
And due to the nature of Ruby obj could be an instance of a different  
class, or of the same class but with different API than before. You  
check it always.

There’s a gotcha with core classes though, like Hash and friends. If  
you for example subclass Hash, MyHash, and define a custom #[]= that’s  
not seen by the rest of inherited methods. So for example an inherited  
MyHash#merge! is still going to use the original Hash#[]=.

Reason is Hash is implemented in C and #merge! has hard-coded calls to  
the C function that implements Hash#[]=. So polymorphism is missing  
here. Same with String and others.

It feels a bit strange in an otherwise pure OO language like Ruby, but  
as far as I know it is a pragmatic compromise for speed. Rubinius and  
JRuby would naturally support that polymorphism, but they don’t on  
purpose for compatibility with MRI.
