static VALUE get_array(VALUE obj, int idx) { VALUE ary = RSTRUCT_GET(obj, idx); if (!RB_TYPE_P(ary, T_ARRAY)) { rb_raise(rb_eTypeError, "%+"PRIsVALUE" not initialized", obj); } return ary; }
inline static VALUE f_signbit(VALUE x) { if (RB_TYPE_P(x, T_FLOAT)) { double f = RFLOAT_VALUE(x); return f_boolcast(!isnan(f) && signbit(f)); } return f_negative_p(x); }
void rb_print_undef(VALUE klass, ID id, int scope) { const char *v = method_scope_name(scope); rb_name_error(id, "undefined%s method `%"PRIsVALUE"' for %s `% "PRIsVALUE"'", v, QUOTE_ID(id), (RB_TYPE_P(klass, T_MODULE)) ? "module" : "class", rb_class_name(klass)); }
static VALUE get_strpath(VALUE obj) { VALUE strpath; strpath = rb_ivar_get(obj, id_at_path); if (!RB_TYPE_P(strpath, T_STRING)) rb_raise(rb_eTypeError, "unexpected @path"); return strpath; }
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type) { if (!RB_TYPE_P(obj, T_DATA) || !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { return 0; } return 1; }
inline static VALUE f_one_p(VALUE x) { if (RB_TYPE_P(x, T_FIXNUM)) { return f_boolcast(FIX2LONG(x) == 1); } else if (RB_TYPE_P(x, T_BIGNUM)) { return Qfalse; } else if (RB_TYPE_P(x, T_RATIONAL)) { VALUE num = RRATIONAL(x)->num; VALUE den = RRATIONAL(x)->den; return f_boolcast(FIXNUM_P(num) && FIX2LONG(num) == 1 && FIXNUM_P(den) && FIX2LONG(den) == 1); } return rb_funcall(x, id_eqeq_p, 1, ONE); }
static const char * ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign) { VALUE value, result = (VALUE)fp->_bf._base; rb_encoding *enc; char *cp; if (valsize != sizeof(VALUE)) return 0; value = *(VALUE *)valp; if (RBASIC(result)->klass) { rb_raise(rb_eRuntimeError, "rb_vsprintf reentered"); } if (sign == '+') { if (RB_TYPE_P(value, T_CLASS)) { # define LITERAL(str) (*sz = rb_strlen_lit(str), str) if (value == rb_cNilClass) { return LITERAL("nil"); } else if (value == rb_cFixnum) { return LITERAL("Fixnum"); } else if (value == rb_cSymbol) { return LITERAL("Symbol"); } else if (value == rb_cTrueClass) { return LITERAL("true"); } else if (value == rb_cFalseClass) { return LITERAL("false"); } # undef LITERAL } value = rb_inspect(value); } else { value = rb_obj_as_string(value); if (sign == ' ') value = QUOTE(value); } enc = rb_enc_compatible(result, value); if (enc) { rb_enc_associate(result, enc); } else { enc = rb_enc_get(result); value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc, ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE, Qnil); *(volatile VALUE *)valp = value; } StringValueCStr(value); RSTRING_GETMEM(value, cp, *sz); ((rb_printf_buffer_extra *)fp)->value = value; OBJ_INFECT(result, value); return cp; }
static long num_members(VALUE klass) { VALUE members; members = struct_ivar_get(klass, id_members); if (!RB_TYPE_P(members, T_ARRAY)) { rb_raise(rb_eTypeError, "broken members"); } return RARRAY_LEN(members); }
void rb_print_inaccessible(VALUE klass, ID id, int scope) { const char *v = method_scope_name(scope); rb_name_error(id, "method `%"PRIsVALUE"' for %s `% "PRIsVALUE"' is %s", QUOTE_ID(id), (RB_TYPE_P(klass, T_MODULE)) ? "module" : "class", rb_class_name(klass), v); }
static VALUE big(VALUE x) { if (FIXNUM_P(x)) return rb_int2big(FIX2LONG(x)); if (RB_TYPE_P(x, T_BIGNUM)) return x; rb_raise(rb_eTypeError, "can't convert %s to Bignum", rb_obj_classname(x)); }
/*! * Returns the singleton class of \a obj. Creates it if necessary. * * \param obj an arbitrary object. * \throw TypeError if \a obj is a Fixnum or a Symbol. * \return the singleton class. * * \post \a obj has its own singleton class. * \post if \a obj is a class, * the returned singleton class also has its own * singleton class in order to keep consistency of the * inheritance structure of metaclasses. * \note a new singleton class will be created * if \a obj does not have it. * \note the singleton classes for nil, true and false are: * NilClass, TrueClass and FalseClass. */ VALUE rb_singleton_class(VALUE obj) { VALUE klass = singleton_class_of(obj); /* ensures an exposed class belongs to its own eigenclass */ if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass); return klass; }
/* * call-seq: * Encoding.find(string) -> enc * * Search the encoding with specified <i>name</i>. * <i>name</i> should be a string. * * Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII> * * Names which this method accept are encoding names and aliases * including following special aliases * * "external":: default external encoding * "internal":: default internal encoding * "locale":: locale encoding * "filesystem":: filesystem encoding * * An ArgumentError is raised when no encoding with <i>name</i>. * Only <code>Encoding.find("internal")</code> however returns nil * when no encoding named "internal", in other words, when Ruby has no * default internal encoding. */ static VALUE enc_find(VALUE klass, VALUE enc) { int idx; if (RB_TYPE_P(enc, T_DATA) && is_data_encoding(enc)) return enc; idx = str_to_encindex(enc); if (idx == UNSPECIFIED_ENCODING) return Qnil; return rb_enc_from_encoding_index(idx); }
VALUE rb_struct_aref(VALUE s, VALUE idx) { long i; if (RB_TYPE_P(idx, T_STRING) || RB_TYPE_P(idx, T_SYMBOL)) { return rb_struct_aref_id(s, rb_to_id(idx)); } i = NUM2LONG(idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)", i, RSTRUCT_LEN(s)); if (RSTRUCT_LEN(s) <= i) rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)", i, RSTRUCT_LEN(s)); return RSTRUCT_PTR(s)[i]; }
VALUE rb_profile_frame_classpath(VALUE frame) { VALUE klass = rb_iseq_klass(frame2iseq(frame)); if (klass && !NIL_P(klass)) { if (RB_TYPE_P(klass, T_ICLASS)) { klass = RBASIC(klass)->klass; } else if (FL_TEST(klass, FL_SINGLETON)) { klass = rb_ivar_get(klass, id__attached__); if (!RB_TYPE_P(klass, T_CLASS)) return rb_sprintf("#<%s:%p>", rb_class2name(rb_obj_class(klass)), (void*)klass); } return rb_class_path(klass); } else { return Qnil; } }
static void add_event_call_back(VALUE obj, VALUE event, VALUE data) { VALUE events = rb_ivar_get(obj, id_events); if (NIL_P(events) || !RB_TYPE_P(events, T_ARRAY)) { events = rb_ary_new(); rb_ivar_set(obj, id_events, events); } ole_delete_event(events, event); rb_ary_push(events, data); }
VALUE rb_check_backtrace(VALUE bt) { long i; static const char err[] = "backtrace must be Array of String"; if (!NIL_P(bt)) { if (RB_TYPE_P(bt, T_STRING)) return rb_ary_new3(1, bt); if (rb_backtrace_p(bt)) return bt; if (!RB_TYPE_P(bt, T_ARRAY)) { rb_raise(rb_eTypeError, err); } for (i=0;i<RARRAY_LEN(bt);i++) { if (!RB_TYPE_P(RARRAY_PTR(bt)[i], T_STRING)) { rb_raise(rb_eTypeError, err); } } } return bt; }
static VALUE syserr_initialize(int argc, VALUE *argv, VALUE self) { #if !defined(_WIN32) char *strerror(); #endif const char *err; VALUE mesg, error, func; VALUE klass = rb_obj_class(self); if (klass == rb_eSystemCallError) { st_data_t data = (st_data_t)klass; rb_scan_args(argc, argv, "12", &mesg, &error, &func); if (argc == 1 && FIXNUM_P(mesg)) { error = mesg; mesg = Qnil; } if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) { klass = (VALUE)data; /* change class */ if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */ rb_raise(rb_eTypeError, "invalid instance type"); } RBASIC_SET_CLASS(self, klass); } } else { rb_scan_args(argc, argv, "02", &mesg, &func); error = rb_const_get(klass, rb_intern("Errno")); } if (!NIL_P(error)) err = strerror(NUM2INT(error)); else err = "unknown error"; if (!NIL_P(mesg)) { rb_encoding *le = rb_locale_encoding(); VALUE str = StringValue(mesg); rb_encoding *me = rb_enc_get(mesg); if (NIL_P(func)) mesg = rb_sprintf("%s - %"PRIsVALUE, err, mesg); else mesg = rb_sprintf("%s @ %"PRIsVALUE" - %"PRIsVALUE, err, func, mesg); if (le != me && rb_enc_asciicompat(me)) { le = me; }/* else assume err is non ASCII string. */ OBJ_INFECT(mesg, str); rb_enc_associate(mesg, le); } else { mesg = rb_str_new2(err); rb_enc_associate(mesg, rb_locale_encoding()); } rb_call_super(1, &mesg); rb_iv_set(self, "errno", error); return self; }
static void fontSetNameHelper(VALUE self, int argc, VALUE *argv, const char *nameIv, char *outBuf, size_t outLen) { rb_check_argc(argc, 1); VALUE arg = argv[0]; // Fixme: in RGSS3, specifying "" (and only that) as font name results in // no text being drawn (everything else is substituted with Arial I think) strncpy(outBuf, "", outLen); if (RB_TYPE_P(arg, RUBY_T_STRING)) { strncpy(outBuf, RSTRING_PTR(arg), outLen); } else if (RB_TYPE_P(arg, RUBY_T_ARRAY)) { for (long i = 0; i < RARRAY_LEN(arg); ++i) { VALUE str = rb_ary_entry(arg, i); /* Non-string objects are tolerated (ignored) */ if (!RB_TYPE_P(str, RUBY_T_STRING)) continue; const char *family = RSTRING_PTR(str); /* We only set the core Font object's name attribute * to the actually existing font name */ if (!shState->fontState().fontPresent(family)) continue; strncpy(outBuf, family, outLen); } } /* RMXP doesn't even care if the argument type is * something other than string/array. Whatever... */ rb_iv_set(self, nameIv, arg); }
static VALUE rb_struct_eql(VALUE s, VALUE s2) { if (s == s2) return Qtrue; if (!RB_TYPE_P(s2, T_STRUCT)) return Qfalse; if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse; if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) { rb_bug("inconsistent struct"); /* should never happen */ } return rb_exec_recursive_paired(recursive_eql, s, s2, s2); }
/* :nodoc: */ static VALUE nucomp_coerce(VALUE self, VALUE other) { if (k_numeric_p(other) && f_real_p(other)) return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self); if (RB_TYPE_P(other, T_COMPLEX)) return rb_assoc_new(other, self); rb_raise(rb_eTypeError, "%s can't be coerced into %s", rb_obj_classname(other), rb_obj_classname(self)); return Qnil; }
static VALUE uscore_get(void) { VALUE line; line = rb_lastline_get(); if (!RB_TYPE_P(line, T_STRING)) { rb_raise(rb_eTypeError, "$_ value need to be String (%s given)", NIL_P(line) ? "nil" : rb_obj_classname(line)); } return line; }
static VALUE class2path(VALUE klass) { VALUE path = rb_class_path(klass); const char *n; n = must_not_be_anonymous((RB_TYPE_P(klass, T_CLASS) ? "class" : "module"), path); if (rb_path_to_class(path) != rb_class_real(klass)) { rb_raise(rb_eTypeError, "%s can't be referred to", n); } return path; }
static void unknown_keyword_error(VALUE hash, const ID *table, int keywords) { VALUE keys; int i; for (i = 0; i < keywords; i++) { rb_hash_delete(hash, ID2SYM(table[i])); } keys = rb_funcall(hash, rb_intern("keys"), 0, 0); if (!RB_TYPE_P(keys, T_ARRAY)) rb_raise(rb_eArgError, "unknown keyword"); keyword_error("unknown", keys); }
void rb_alias(VALUE klass, ID name, ID def) { VALUE target_klass = klass; VALUE defined_class; rb_method_entry_t *orig_me; rb_method_flag_t flag = NOEX_UNDEF; if (NIL_P(klass)) { rb_raise(rb_eTypeError, "no class to make alias"); } rb_frozen_class_p(klass); again: orig_me = search_method(klass, def, &defined_class); if (UNDEFINED_METHOD_ENTRY_P(orig_me) || UNDEFINED_REFINED_METHOD_P(orig_me->def)) { if ((!RB_TYPE_P(klass, T_MODULE)) || (orig_me = search_method(rb_cObject, def, 0), UNDEFINED_METHOD_ENTRY_P(orig_me))) { rb_print_undef(klass, def, 0); } } if (orig_me->def->type == VM_METHOD_TYPE_ZSUPER) { klass = RCLASS_SUPER(klass); def = orig_me->def->original_id; flag = orig_me->flag; goto again; } if (RB_TYPE_P(defined_class, T_ICLASS)) { VALUE real_class = RBASIC_CLASS(defined_class); if (real_class && RCLASS_ORIGIN(real_class) == defined_class) defined_class = real_class; } if (flag == NOEX_UNDEF) flag = orig_me->flag; method_entry_set(target_klass, name, orig_me, flag, defined_class); }
VALUE rb_population_standard_deviation(VALUE self) { unsigned int array_length = rb_long2int(RARRAY_LEN(self)); if (RB_TYPE_P(self, T_ARRAY) == 0) { rb_raise(rb_eTypeError, "can only be used on an array"); } if (array_length <= 1) { rb_raise(rb_eRangeError, "array must have more than one element"); } return DBL2NUM(sqrt(calculate_total_distance_from_mean(self, array_length) / array_length)); }
inline static VALUE f_signbit(VALUE x) { #if defined(HAVE_SIGNBIT) && defined(__GNUC__) && defined(__sun) && \ !defined(signbit) extern int signbit(double); #endif if (RB_TYPE_P(x, T_FLOAT)) { double f = RFLOAT_VALUE(x); return f_boolcast(!isnan(f) && signbit(f)); } return f_negative_p(x); }
/* * call-seq: * WIN32OLE_EVENT#off_event([event]) * * removes the callback of event. * * ie = WIN32OLE.new('InternetExplorer.Application') * ev = WIN32OLE_EVENT.new(ie) * ev.on_event('BeforeNavigate2') {|*args| * args.last[6] = true * } * ... * ev.off_event('BeforeNavigate2') * ... */ static VALUE fev_off_event(int argc, VALUE *argv, VALUE self) { VALUE event = Qnil; VALUE events; rb_scan_args(argc, argv, "01", &event); if(!NIL_P(event)) { if(!RB_TYPE_P(event, T_STRING) && !RB_TYPE_P(event, T_SYMBOL)) { rb_raise(rb_eTypeError, "wrong argument type (expected String or Symbol)"); } if (RB_TYPE_P(event, T_SYMBOL)) { event = rb_sym_to_s(event); } } events = rb_ivar_get(self, id_events); if (NIL_P(events)) { return Qnil; } ole_delete_event(events, event); return Qnil; }
static VALUE ev_on_event(int argc, VALUE *argv, VALUE self, VALUE is_ary_arg) { struct oleeventdata *poleev; VALUE event, args, data; Data_Get_Struct(self, struct oleeventdata, poleev); if (poleev->pConnectionPoint == NULL) { rb_raise(eWIN32OLERuntimeError, "IConnectionPoint not found. You must call advise at first."); } rb_scan_args(argc, argv, "01*", &event, &args); if(!NIL_P(event)) { if(!RB_TYPE_P(event, T_STRING) && !RB_TYPE_P(event, T_SYMBOL)) { rb_raise(rb_eTypeError, "wrong argument type (expected String or Symbol)"); } if (RB_TYPE_P(event, T_SYMBOL)) { event = rb_sym_to_s(event); } } data = rb_ary_new3(4, rb_block_proc(), event, args, is_ary_arg); add_event_call_back(self, event, data); return Qnil; }
static VALUE range_loader(VALUE range, VALUE obj) { if (!RB_TYPE_P(obj, T_OBJECT) || RBASIC(obj)->klass != rb_cObject) { rb_raise(rb_eTypeError, "not a dumped range object"); } range_modify(range); RANGE_SET_BEG(range, rb_ivar_get(obj, id_beg)); RANGE_SET_END(range, rb_ivar_get(obj, id_end)); RANGE_SET_EXCL(range, rb_ivar_get(obj, id_excl)); return range; }
VALUE rb_struct_s_members(VALUE klass) { VALUE members = struct_ivar_get(klass, id_members); if (NIL_P(members)) { rb_raise(rb_eTypeError, "uninitialized struct"); } if (!RB_TYPE_P(members, T_ARRAY)) { rb_raise(rb_eTypeError, "corrupted struct"); } return members; }