From 9166dd9ec7189376a9c1cb3dda2e40628096d015 Mon Sep 17 00:00:00 2001 From: matz Date: Sun, 16 Aug 2009 22:28:48 +0000 Subject: [PATCH] * numeric.c (num_imaginary): num#i to return imaginary counterpart of the given numeric. * complex.c (Init_Complex): undef #i for complex numbers. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24561 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ complex.c | 1 + numeric.c | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/ChangeLog b/ChangeLog index 36f8273da7..8308ccdb6a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Aug 17 07:16:10 2009 Yukihiro Matsumoto + + * numeric.c (num_imaginary): num#i to return imaginary counterpart + of the given numeric. + + * complex.c (Init_Complex): undef #i for complex numbers. + Mon Aug 17 00:17:33 2009 Tadayoshi Funaba * lib/complex.rb, lib/rational.rb: added warning messages. diff --git a/complex.c b/complex.c index ee281ba819..d32ecfd3ca 100644 --- a/complex.c +++ b/complex.c @@ -1906,6 +1906,7 @@ Init_Complex(void) rb_undef_method(rb_cComplex, "round"); rb_undef_method(rb_cComplex, "step"); rb_undef_method(rb_cComplex, "truncate"); + rb_undef_method(rb_cComplex, "i"); #if 0 /* NUBY */ rb_undef_method(rb_cComplex, "//"); diff --git a/numeric.c b/numeric.c index ecaf9c503b..4ba69fd5d9 100644 --- a/numeric.c +++ b/numeric.c @@ -236,6 +236,21 @@ num_uplus(VALUE num) return num; } +/* + * call-seq: + * num.i -> Complex(0,num) + * + * Returns the corresponding imaginary number. + * Not available for complex numbers. + */ + +static VALUE +num_imaginary(VALUE num) +{ + return rb_complex_new(INT2FIX(0), num); +} + + /* * call-seq: * -num -> numeric @@ -3154,6 +3169,7 @@ Init_Numeric(void) rb_define_method(rb_cNumeric, "initialize_copy", num_init_copy, 1); rb_define_method(rb_cNumeric, "coerce", num_coerce, 1); + rb_define_method(rb_cNumeric, "i", num_imaginary, 0); rb_define_method(rb_cNumeric, "+@", num_uplus, 0); rb_define_method(rb_cNumeric, "-@", num_uminus, 0); rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);