There are several Ruby C API functions for running some Ruby code. Most just run the code in an isolated binding like require
does. But some of them first wrap the code in an anonymous module before running it. For example, rb_load
takes an argument for whether you want this wrapping, rb_eval_string_wrap
is just rb_eval_string_protect
but with wrapping.
In C, the wrapping looks like this:
/* load in anonymous module as toplevel */
th->top_self = rb_obj_clone(rb_vm_top_self());
th->top_wrapper = rb_module_new();
rb_extend_object(th->top_self, th->top_wrapper);
What is the point of doing this? I've tested these functions alongside their unwrapped equivalents and the result is always the same. Is there some use-case I'm not seeing?