| 238 | } |
| 239 | |
| 240 | bool RubyEngine::hasMethod(ScriptObject& methodObject, std::string_view methodName, bool overriden_only) { |
| 241 | auto val = std::any_cast<VALUE>(methodObject.object); |
| 242 | ID method_id = rb_intern(methodName.data()); |
| 243 | if (rb_respond_to(val, method_id) == 0) { |
| 244 | return false; |
| 245 | } |
| 246 | if (!overriden_only) { |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | // I'd have prefered to do the equivalent of `instance_obj.method(:methodName).owner == instance_obj.class` but that is borderline impossible |
| 251 | // Instead, this is equivalent to `instance_obj.class.instance_methods(false).include?(:methodName)` |
| 252 | VALUE klass = rb_obj_class(val); |
| 253 | // include_methods_from_ancestors: false; |
| 254 | VALUE args[1] = {Qfalse}; |
| 255 | VALUE methods_without_ancestors = rb_class_instance_methods(1, args, klass); |
| 256 | return rb_ary_includes(methods_without_ancestors, ID2SYM(method_id)) == Qtrue; |
| 257 | } |
| 258 | |
| 259 | } // namespace openstudio |
| 260 | |