Hi
I wanted to know about complie-time polymorphsim
and run-time polymorpshism in ruby
with examples.
Regards,
Hari
Hi
I wanted to know about complie-time polymorphsim
and run-time polymorpshism in ruby
with examples.
Regards,
Hari
On Thu, Jul 8, 2010 at 12:55 PM, hari bhat [email protected] 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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs