mrb_obj_respond_to

Function mrb_obj_respond_to 

Source
pub unsafe extern "C" fn mrb_obj_respond_to(
    mrb: *mut mrb_state,
    c: *mut RClass,
    mid: mrb_sym,
) -> mrb_bool
Expand description

Returns true if obj responds to the given method. If the method was defined for that class it returns true, it returns false otherwise.

 Example:
 class ExampleClass
   def example_method
   end
 end

 ExampleClass.new.respond_to?(:example_method) # => true

 // C style
 void
 mrb_example_gem_init(mrb_state* mrb) {
   struct RClass *example_class;
   mrb_sym mid;
   mrb_bool obj_resp;

   example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
   mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE());
   mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" ));
   obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => TRUE (true in Ruby world)

   // If mrb_obj_respond_to returns TRUE then puts "True"
   // If mrb_obj_respond_to returns FALSE then puts "False"
   if (obj_resp) {
     puts("True");
   }
   else {
     puts("False");
   }
 }

@param mrb The current mruby state. @param c A reference to a class. @param mid A symbol referencing a method id. @return mrb_bool A boolean value.