Created
June 21, 2012 06:43
-
-
Save code-later/2964264 to your computer and use it in GitHub Desktop.
Is Ruby's put implemented with method_missing?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The 'rb_define_global_function' can be found in class.c | |
/*! | |
* Defines a global function | |
* \param name name of the function | |
* \param func the method body | |
* \param argc the number of parameters, or -1 or -2. see \ref defmethod. | |
*/ | |
void | |
rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc) | |
{ | |
rb_define_module_function(rb_mKernel, name, func, argc); | |
} | |
// As you can see, this just defines a method on the "Kernel"-Module (which is defined in object.c) | |
/*! | |
* Defines a module function for \a module. | |
* \param module an module or a class. | |
* \param name name of the function | |
* \param func the method body | |
* \param argc the number of parameters, or -1 or -2. see \ref defmethod. | |
*/ | |
void | |
rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc) | |
{ | |
rb_define_private_method(module, name, func, argc); | |
rb_define_singleton_method(module, name, func, argc); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In io.c the method 'puts' is defined as global function | |
rb_define_global_function("puts", rb_f_puts, -1); | |
// rb_f_puts is defined in io.c | |
static VALUE | |
rb_f_puts(int argc, VALUE *argv, VALUE recv) | |
{ | |
if (recv == rb_stdout) { | |
return rb_io_puts(argc, argv, recv); | |
} | |
return rb_funcall2(rb_stdout, rb_intern("puts"), argc, argv); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def method_missing(meth, *args, &blk) | |
STDOUT.puts "mm: #{meth}" | |
end | |
this_method_is_not_there | |
puts "test" | |
# Output: | |
# > mm: this_method_is_not_there | |
# > test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment