diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-27 11:56:03 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-04-27 11:56:03 +0000 |
commit | d5a7299bca633621789db8ff76665d0caee80918 (patch) | |
tree | 0c71b9a6291d3c2b407e44d63f220777d7478308 /numeric.c | |
parent | e5b436cd76961741a3ba59e3c825f0b6e17864cb (diff) | |
download | ruby-d5a7299bca633621789db8ff76665d0caee80918.tar.gz |
{Fixnum,Bignum}#[] is unified into Integer.
* numeric.c (int_aref): {Fixnum,Bignum}#[] is unified into
Integer.
* bignum.c (rb_big_aref): Don't define Bignum#<<.
* internal.h (rb_big_aref): Declared.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 48 |
1 files changed, 33 insertions, 15 deletions
@@ -4025,20 +4025,6 @@ rb_int_rshift(VALUE x, VALUE y) return Qnil; } -/* - * call-seq: - * fix[n] -> 0, 1 - * - * Bit Reference---Returns the +n+th bit in the binary representation of - * +fix+, where <code>fix[0]</code> is the least significant bit. - * - * For example: - * - * a = 0b11001100101010 - * 30.downto(0) do |n| print a[n] end - * #=> 0000000000000000011001100101010 - */ - static VALUE fix_aref(VALUE fix, VALUE idx) { @@ -4068,6 +4054,38 @@ fix_aref(VALUE fix, VALUE idx) /* * call-seq: + * fix[n] -> 0, 1 + * + * Bit Reference---Returns the +n+th bit in the binary representation of + * +fix+, where <code>fix[0]</code> is the least significant bit. + * + * For example: + * + * a = 0b11001100101010 + * 30.downto(0) do |n| print a[n] end + * #=> 0000000000000000011001100101010 + * + * a = 9**15 + * 50.downto(0) do |n| + * print a[n] + * end + * #=> 000101110110100000111000011110010100111100010111001 + */ + +static VALUE +int_aref(VALUE num, VALUE idx) +{ + if (FIXNUM_P(num)) { + return fix_aref(num, idx); + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + return rb_big_aref(num, idx); + } + return Qnil; +} + +/* + * call-seq: * int.to_f -> float * * Converts +int+ to a +Float+. If +int+ doesn't fit in a +Float+, @@ -4703,7 +4721,7 @@ Init_Numeric(void) rb_define_method(rb_cFixnum, "&", fix_and, 1); rb_define_method(rb_cFixnum, "|", fix_or, 1); rb_define_method(rb_cFixnum, "^", fix_xor, 1); - rb_define_method(rb_cFixnum, "[]", fix_aref, 1); + rb_define_method(rb_cInteger, "[]", int_aref, 1); rb_define_method(rb_cInteger, "<<", rb_int_lshift, 1); rb_define_method(rb_cInteger, ">>", rb_int_rshift, 1); |