In c extensions to ruby, to call a method you can do
rb_funcall(object, rb_intern("method name"), argumentcount, arg1, arg2, …);
where rb_intern() returns some internal represantation of the method name.
I have seen some code which does instead
ID method;
CONST_ID(method, "method name");
rb_funcall(object, method, argumentcount, arg1, arg2, …);
What exactly is the difference beetwen rb_intern() and CONST_ID. Which advantages does CONST_ID() have?
The
CONST_IDmacro callsrb_intern2(which is about the same asrb_intern) to get the ID, but there is one big difference. If you look at theCONST_IDmacro source ininclude/ruby/ruby.h, you’ll see that it starts a new block and defines a staticIDvariable to cache the result. If, next time that block is executed, the static variable is already set, it just returns the cached result instead of searching for theIDall over again.So they do the same thing, but
CONST_IDshould be faster for multiple lookups of the same string.