static VALUE string_to_r(VALUE self) { VALUE s = f_gsub(self, underscores_pat, an_underscore); VALUE a = string_to_r_internal(s); if (!NIL_P(RARRAY_AT(a, 0))) return RARRAY_AT(a, 0); return rb_rational_new1(INT2FIX(0)); }
static VALUE string_to_r_strict(VALUE self) { VALUE a = string_to_r_internal(self); if (NIL_P(RARRAY_PTR(a)[0]) || RSTRING_LEN(RARRAY_PTR(a)[1]) > 0) { VALUE s = f_inspect(self); rb_raise(rb_eArgError, "invalid value for convert(): %s", StringValuePtr(s)); } return RARRAY_PTR(a)[0]; }
static VALUE string_to_r_strict(VALUE self) { VALUE a = string_to_r_internal(self); if (NIL_P(RARRAY_AT(a, 0)) || RSTRING_LEN(RARRAY_AT(a, 1)) > 0) { VALUE s = f_inspect(self); rb_raise(rb_eArgError, "invalid value for Rational: %s", StringValuePtr(s)); } return RARRAY_AT(a, 0); }
/* * call-seq: * str.to_r -> rational * * Returns a rational which denotes the string form. The parser * ignores leading whitespaces and trailing garbage. Any digit * sequences can be separeted by an underscore. Returns zero for null * or garbage string. * * NOTE: '0.3'.to_r isn't the same as 0.3.to_r. The former is * equivalent to '3/10'.to_r, but the latter isn't so. * * For example: * * ' 2 '.to_r #=> (2/1) * '300/2'.to_r #=> (150/1) * '-9.2'.to_r #=> (-46/5) * '-9.2e2'.to_r #=> (-920/1) * '1_234_567'.to_r #=> (1234567/1) * '21 june 09'.to_r #=> (21/1) * '21/06/09'.to_r #=> (7/2) * 'bwv 1079'.to_r #=> (0/1) */ static VALUE string_to_r(VALUE self, SEL sel) { VALUE s, a, backref; backref = rb_backref_get(); rb_match_busy(backref); s = f_gsub(self, underscores_pat, an_underscore); a = string_to_r_internal(s); rb_backref_set(backref); if (!NIL_P(RARRAY_PTR(a)[0])) return RARRAY_PTR(a)[0]; return rb_rational_new1(INT2FIX(0)); }