Coerce with Rational and Complex

e$B1sF#$H?=$7$^$9!#e(B

e$B0J2<$N5sF0$O0U?^E*$G$7$g$&$+!#e(B

p 1.coerce(Rational(1, 2))  #=> [0.5, 1.0]

e$BD>46E*$K$Oe(B [Rational(1, 2), Rational(1, 1)] e$B$,M_$7$$$G$9!#e(B
e$B$^$?!"0J2<$O$I$&$G$7$g$&$+!#e(B

p 1.coerce(Complex(1, 1))   #=> can't convert 1+1i into Integer

[Complex(1, 0), Complex(1, 1)] e$B$,M_$7$$5$J,$G$9!#e(B

e$B$3$d$D$i$,AH$9~$$K$J$C$?;v$G!“e(Bcoerce e$B$r$$$m$$$m$H9M$(D>$9e(B
e$BI,MW$,$”$k$N$+$b$7$l$^$;$s!#e(B

e$B86$G$9!#e(B

[Complex(1, 0), Complex(1, 1)] e$B$,M_$7$$5$J,$G$9!#e(B

e$B86B’$H$7$F!"e(Bcoerce
e$B$O!"e(B(e$B?7$7$$?te(B).coerce(e$B8E$$?t!K$N$_$,LdBj$J$N$G!"e(B
1.coerce(Rational(1,2))
e$B$N$h$&$J!"e(B(e$B8E$$?te(B).coerce(e$B?7$7$$?t!K$H$$$&e(B
e$B%Q%?!<%s$G$O5/F0$7$J$$$O$:$G$9!#[email protected]$+$i!"JQ$JCM$rJV$7$F$b!“Nc30$G$b$+$^e(B
e$B$o$J$$$G$9!#$b$A$m$s!”$=$l$C$]$$CM$rJV$7$F$b32$O$J$$$G$9$,!#$$$d!"5,Lse(B
e$B$rGK$C$?$7$k$7$KNc30$r=P$7$?J}$,$+$($C$F$$$$$+$J!#e(B

e$B1sF#$G$9!#e(B

08/04/22 e$B$Ke(B Shin-ichiro HARA[email protected]
e$B$5$s$O=q$-$^$7$?e(B:

p 1.coerce(Complex(1, 1))   #=> can't convert 1+1i into Integer

[Complex(1, 0), Complex(1, 1)] e$B$,M_$7$$5$J,$G$9!#e(B

e$B86B’$H$7$F!"e(Bcoerce e$B$O!"e(B(e$B?7$7$$?te(B).coerce(e$B8E$$?t!K$N$_$,LdBj$J$N$G!"e(B
1.coerce(Rational(1,2)) e$B$N$h$&$J!"e(B(e$B8E$$?te(B).coerce(e$B?7$7$$?t!K$H$$$&e(B
e$B%Q%?!<%s$G$O5/F0$7$J$$$O$:$G$9!#[email protected]$+$i!"JQ$JCM$rJV$7$F$b!“Nc30$G$b$+$^e(B
e$B$o$J$$$G$9!#$b$A$m$s!”$=$l$C$]$$CM$rJV$7$F$b32$O$J$$$G$9$,!#$$$d!"5,Lse(B
e$B$rGK$C$?$7$k$7$KNc30$r=P$7$?J}$,$+$($C$F$$$$$+$J!#e(B

e$B$J$k$[$I!"$=$&$$$&@_7W$J$s$G$9$M!#$J$s$H$J$/$o$+$j$^$7$?!#e(B

e$B0l1~$=$l$C$]$$CM$rJV$9%Q%C%A$r$$$$2C8:$K=q$$$F$_$^$7$?!#e(B

Index: numeric.c

— numeric.c (revision 16157)
+++ numeric.c (working copy)
@@ -97,6 +97,8 @@
}

+static int do_coerce(VALUE *x, VALUE y, int err);
+
/

  • call-seq:
  • num.coerce(numeric)   => array
    

@@ -116,9 +118,14 @@
static VALUE
num_coerce(VALUE x, VALUE y)
{

  • static int recurse = 0;
    if (CLASS_OF(x) == CLASS_OF(y))
    return rb_assoc_new(y, x);
  • return rb_assoc_new(rb_Float(y), rb_Float(x));
  • if (recurse) return Qnil;
  • recurse++;
  • do_coerce(&x, &y, Qtrue);
  • recurse–;
  • return rb_assoc_new(y, x);
    }

static VALUE

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs