Last Updated: February 25, 2016
·
1.481K
· we4tech

Different types of methods in ruby

I was quite surprised seeing how ruby deal with different types of methods. After digging in ruby source code I’ve found out the following types of ruby methods -

This flag is used to any ruby method which is referenced from source code.

VM_METHOD_TYPE_ISEQ

Method which is created on behalf of native C function

VM_METHOD_TYPE_CFUNC

When we are declaring attribute setter using “attr_writer” this flag is used.

VM_METHOD_TYPE_ATTRSET

When we are declaring attribute getter using “attar_reader” this flag is used.

VM_METHOD_TYPE_IVAR

When we are declaring method using “define_method” and passing proc for method body this flag is used.

VM_METHOD_TYPE_BMETHOD

If we are explicitly declaring method scope (private, public or protected) from the class hierarchy, ruby will redeclare method in the child class and mark that one with this flag.

VM_METHOD_TYPE_ZSUPER,

When any method is undef using (undef_method or undef) will be marked with this flag. (Still method exists but won’t respond to)

VM_METHOD_TYPE_UNDEF,

If not implemented c function (which actually refers to rbfnotimplement function)

VM_METHOD_TYPE_NOTIMPLEMENTED,

This flag is used for separating few set of methods from rest. so that VM can handle them better and optimized way.

VM_METHOD_TYPE_OPTIMIZED, /* Kernel#send, Proc#call, etc */

When method_missing is declared and none existing method falls into this loop. This missing method is dynamically generated and flagged as this.

VM_METHOD_TYPE_MISSING, /* wrapper for method_missing(id) */

This is new feature and only introduced in 2.0.0, This one is used when we are using refinement.

VM_METHOD_TYPE_REFINED

See there are 11 types of methods in ruby. I didn’t know that before. Now i know :)

I cross post on my blog - http://we4tech.wordpress.com/2013/05/09/different-type-methods-in-ruby/ :)