[前][次][番号順一覧][スレッド一覧]

ruby-changes:42105

From: mrkn <ko1@a...>
Date: Fri, 18 Mar 2016 23:52:54 +0900 (JST)
Subject: [ruby-changes:42105] mrkn:r54179 (trunk): * numeric.c (int_to_f, fix_to_f): rename fix_to_f to int_to_f, and add

mrkn	2016-03-18 23:52:46 +0900 (Fri, 18 Mar 2016)

  New Revision: 54179

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54179

  Log:
    * numeric.c (int_to_f, fix_to_f): rename fix_to_f to int_to_f, and add
      treatment for subclasses which don't have definitions of to_f method.
    
    * numeric.c (Integer#to_f, Fixnum#to_f): move to_f method from Fixnum
      to Integer.
    
    * ext/-test-/integer/my_integer.rb: define helper class for testing
      to_f method for a subclass of Integer.
    
    * ext/-test-/integer/extconf.rb: ditto.
    
    * ext/-test-/integer/init.c: ditto.
    
    * test/-ext-/integer/test_my_integer.rb: examine to_f method for a
      subclass of Integer.

  Added directories:
    trunk/ext/-test-/integer/
    trunk/test/-ext-/integer/
  Added files:
    trunk/ext/-test-/integer/extconf.rb
    trunk/ext/-test-/integer/init.c
    trunk/ext/-test-/integer/my_integer.c
    trunk/test/-ext-/integer/test_my_integer.rb
  Modified files:
    trunk/ChangeLog
    trunk/numeric.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 54178)
+++ numeric.c	(revision 54179)
@@ -3765,18 +3765,23 @@ fix_aref(VALUE fix, VALUE idx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3765
 
 /*
  *  call-seq:
- *     fix.to_f  ->  float
+ *     int.to_f  ->  float
  *
- *  Converts +fix+ to a Float.
+ *  Converts +int+ to a Float.
  *
  */
 
 static VALUE
-fix_to_f(VALUE num)
+int_to_f(VALUE num)
 {
     double val;
 
-    val = (double)FIX2LONG(num);
+    if (FIXNUM_P(num)) {
+	val = (double)FIX2LONG(num);
+    }
+    else {
+	rb_raise(rb_eTypeError, "Unknown subclass for to_f: %s", rb_obj_classname(num));
+    }
 
     return DBL2NUM(val);
 }
@@ -4214,6 +4219,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4219
     rb_define_method(rb_cInteger, "ord", int_ord, 0);
     rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
     rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
+    rb_define_method(rb_cInteger, "to_f", int_to_f, 0);
     rb_define_method(rb_cInteger, "floor", int_to_i, 0);
     rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
     rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
@@ -4253,7 +4259,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4259
     rb_define_method(rb_cFixnum, "<<", rb_fix_lshift, 1);
     rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1);
 
-    rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0);
     rb_define_method(rb_cFixnum, "size", fix_size, 0);
     rb_define_method(rb_cFixnum, "bit_length", rb_fix_bit_length, 0);
     rb_define_method(rb_cFixnum, "succ", fix_succ, 0);
Index: test/-ext-/integer/test_my_integer.rb
===================================================================
--- test/-ext-/integer/test_my_integer.rb	(revision 0)
+++ test/-ext-/integer/test_my_integer.rb	(revision 54179)
@@ -0,0 +1,26 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/integer/test_my_integer.rb#L1
+# frozen_string_literal: false
+require 'test/unit'
+require "-test-/integer"
+
+class TestIntegerExt < Test::Unit::TestCase
+  def test_my_integer_to_f
+    assert_raise(TypeError) do
+      Bug::Integer::MyInteger.new.to_f
+    end
+
+    begin
+      Bug::Integer::MyInteger.class_eval do
+        def to_f
+        end
+      end
+
+      assert_nothing_raised do
+        Bug::Integer::MyInteger.new.to_f
+      end
+    ensure
+      Bug::Integer::MyInteger.class_eval do
+        remove_method :to_f
+      end
+    end
+  end
+end
Index: ext/-test-/integer/my_integer.c
===================================================================
--- ext/-test-/integer/my_integer.c	(revision 0)
+++ ext/-test-/integer/my_integer.c	(revision 54179)
@@ -0,0 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/integer/my_integer.c#L1
+#include "ruby.h"
+
+static VALUE
+my_integer_s_new(VALUE klass)
+{
+    return Data_Wrap_Struct(klass, 0, 0, 0);
+}
+
+void
+Init_my_integer(VALUE klass)
+{
+    VALUE cMyInteger;
+
+    cMyInteger = rb_define_class_under(klass, "MyInteger", rb_cInteger);
+    rb_define_singleton_method(cMyInteger, "new", my_integer_s_new, 0);
+}
Index: ext/-test-/integer/init.c
===================================================================
--- ext/-test-/integer/init.c	(revision 0)
+++ ext/-test-/integer/init.c	(revision 54179)
@@ -0,0 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/integer/init.c#L1
+#include "ruby.h"
+
+#define init(n) {void Init_##n(VALUE klass); Init_##n(klass);}
+
+void
+Init_integer(void)
+{
+    VALUE mBug = rb_define_module("Bug");
+    VALUE klass = rb_define_class_under(mBug, "Integer", rb_cObject);
+    TEST_INIT_FUNCS(init);
+}
Index: ext/-test-/integer/extconf.rb
===================================================================
--- ext/-test-/integer/extconf.rb	(revision 0)
+++ ext/-test-/integer/extconf.rb	(revision 54179)
@@ -0,0 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/integer/extconf.rb#L1
+# frozen_string_literal: false
+$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
+$srcs = Dir[File.join($srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
+inits = $srcs.map {|s| File.basename(s, ".*")}
+inits.delete("init")
+inits.map! {|s|"X(#{s})"}
+$defs << "-DTEST_INIT_FUNCS(X)=\"#{inits.join(' ')}\""
+create_makefile("-test-/integer")
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54178)
+++ ChangeLog	(revision 54179)
@@ -1,3 +1,21 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Mar 18 23:41:00 2016  Kenta Murata  <mrkn@m...>
+
+	* numeric.c (int_to_f, fix_to_f): rename fix_to_f to int_to_f, and add
+	  treatment for subclasses which don't have definitions of to_f method.
+
+	* numeric.c (Integer#to_f, Fixnum#to_f): move to_f method from Fixnum
+	  to Integer.
+
+	* ext/-test-/integer/my_integer.rb: define helper class for testing
+	  to_f method for a subclass of Integer.
+
+	* ext/-test-/integer/extconf.rb: ditto.
+
+	* ext/-test-/integer/init.c: ditto.
+
+	* test/-ext-/integer/test_my_integer.rb: examine to_f method for a
+	  subclass of Integer.
+
 Fri Mar 18 22:32:00 2016  Kenta Murata  <mrkn@m...>
 
 	* include/ruby/intern.h (rb_big_hash): Move to internal.h.

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]