summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/bigdecimal.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-31 13:45:31 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-05-31 13:45:31 +0000
commitb54141bb1b4ae96ff6300177e5eb309b5fee738f (patch)
treec07719510f9e94164e32e3901f3b12e0a12d846f /ext/bigdecimal/bigdecimal.c
parent899d2c14b055e180308396b5ef36397c0737f6f7 (diff)
* ext/bigdecimal/bigdecimal.c (BigDecimal_new): support instantiation a
BigDecimal object from an Integer. * test/bigdecimal/test_bigdecimal.rb (test_new_with_integer): add for testing the above change. * ext/bigdecimal/bigdecimal.c (BigDecimal_global_new): replace its body with a BigDecimal_new call. * test/bigdecimal/test_bigdecimal.rb (test_global_new_with_integer): add for testing the above change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31863 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/bigdecimal.c')
-rw-r--r--ext/bigdecimal/bigdecimal.c70
1 files changed, 37 insertions, 33 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 7f64288647..45dac45645 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -1738,8 +1738,23 @@ BigDecimal_power(VALUE self, VALUE p)
return ToValue(y);
}
+/* call-seq:
+ * new(initial, digits)
+ *
+ * Create a new BigDecimal object.
+ *
+ * initial:: The initial value, as a String. Spaces are ignored, unrecognized
+ * characters terminate the value.
+ *
+ * digits:: The number of significant digits, as a Fixnum. If omitted or 0,
+ * the number of significant digits is determined from the initial
+ * value.
+ *
+ * The actual number of significant digits used in computation is usually
+ * larger than the specified number.
+ */
static VALUE
-BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
+BigDecimal_new(int argc, VALUE *argv, VALUE self)
{
ENTER(5);
Real *pv;
@@ -1747,45 +1762,34 @@ BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
VALUE nFig;
VALUE iniValue;
- if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
- mf = 0;
- } else {
- mf = GetPositiveInt(nFig);
+ if (rb_scan_args(argc, argv, "11", &iniValue, &nFig) == 1) {
+ mf = 0;
+ }
+ else {
+ mf = GetPositiveInt(nFig);
+ }
+
+ switch (TYPE(iniValue)) {
+ case T_FIXNUM:
+ /* fall through */
+ case T_BIGNUM:
+ return ToValue(GetVpValue(iniValue, 1));
+
+ case T_STRING:
+ /* fall through */
+ default:
+ break;
}
SafeStringValue(iniValue);
- GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
+ GUARD_OBJ(pv, VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
+
return ToValue(pv);
}
- /* call-seq:
- * new(initial, digits)
- *
- * Create a new BigDecimal object.
- *
- * initial:: The initial value, as a String. Spaces are ignored, unrecognized characters terminate the value.
- *
- * digits:: The number of significant digits, as a Fixnum. If omitted or 0, the number of significant digits is determined from the initial value.
- *
- * The actual number of significant digits used in computation is usually
- * larger than the specified number.
- */
static VALUE
-BigDecimal_new(int argc, VALUE *argv, VALUE self)
+BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
{
- ENTER(5);
- Real *pv;
- size_t mf;
- VALUE nFig;
- VALUE iniValue;
-
- if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
- mf = 0;
- } else {
- mf = GetPositiveInt(nFig);
- }
- SafeStringValue(iniValue);
- GUARD_OBJ(pv,VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
- return ToValue(pv);
+ return BigDecimal_new(argc, argv, rb_cBigDecimal);
}
/* call-seq: