# Suggestion: Integer#hex

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

Integer#hex e\$B\$He(B #oct e\$B\$rDI2C\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B
String#hex e\$B\$?\$A\$NH?E>\$G\$9!#e(B

10000.hex #=> “2710”

irb e\$B\$r7W;;5!Be\$o\$j\$K;H\$&>l9g\$KJXMx\$@\$H;W\$\$\$^\$9!#e(B
10000.to_s(16) e\$B\$O\$A\$g\$C\$HD9\$9\$.\$G\$9\$7!#e(B

# Index: numeric.c

— numeric.c (revision 16716)
+++ numeric.c (working copy)
@@ -3042,6 +3042,36 @@

/*

• call-seq:
• ``````int.hex    => string
``````
• Returns a string of hexadecimal digits; equivalent to int.to_s(16).
• ``````0.hex       #=> "0"
``````
• ``````10000.hex   #=> "2710"
``````
• */
+static VALUE
+int_hex(VALUE num)
+{
• return rb_funcall(num, rb_intern(“to_s”), 1, INT2FIX(16));
+}

+/*

• call-seq:
• ``````int.oct    => string
``````
• Returns a string of octal digits; equivalent to int.to_s(8).
• ``````0.oct       #=> "0"
``````
• ``````10000.oct   #=> "23420"
``````
• */
+static VALUE
+int_oct(VALUE num)
+{
• return rb_funcall(num, rb_intern(“to_s”), 1, INT2FIX(8));
+}

+/*

• call-seq:
• ``````fix.zero?    => true or false
``````
• Returns `true` if fix is zero.
@@ -3163,6 +3193,8 @@
rb_define_method(rb_cInteger, “ceil”, int_to_i, 0);
rb_define_method(rb_cInteger, “truncate”, int_to_i, 0);
rb_define_method(rb_cInteger, “round”, int_round, -1);
• rb_define_method(rb_cInteger, “hex”, int_hex, 0);

• rb_define_method(rb_cInteger, “oct”, int_oct, 0);

rb_cFixnum = rb_define_class(“Fixnum”, rb_cInteger);
rb_include_module(rb_cFixnum, rb_mPrecision);
Index: test/ruby/test_integer.rb
===================================================================
— test/ruby/test_integer.rb (revision 16716)
+++ test/ruby/test_integer.rb (working copy)
@@ -190,4 +190,14 @@
assert_equal(2 ** 50, Integer(2.0 ** 50))
assert_raise(TypeError) { Integer(nil) }
end

• def test_hex

• assert_equal(“0”, 0.hex)

• assert_equal(“2710”, 10000.hex)

• end

• def test_oct

• assert_equal(“0”, 0.oct)

• assert_equal(“23420”, 10000.oct)

• end
end

e\$B\$^\$D\$b\$He(B e\$B\$f\$-\$R\$m\$G\$9e(B

In message “Re: [ruby-dev:34902] Suggestion: Integer#hex”
on Sat, 31 May 2008 19:10:48 +0900, “Yusuke ENDOH” [email protected]
writes:

|Integer#hex e\$B\$He(B #oct e\$B\$rDI2C\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B
|String#hex e\$B\$?\$A\$NH?E>\$G\$9!#e(B
|
| 10000.hex #=> “2710”
|
|irb e\$B\$r7W;;5!Be\$o\$j\$K;H\$&>l9g\$KJXMx\$@\$H;W\$\$\$^\$9!#e(B
|10000.to_s(16) e\$B\$O\$A\$g\$C\$HD9\$9\$.\$G\$9\$7!#e(B

e\$BJXMx\$@\$H\$O;W\$\$\$^\$9\$,!“F1\$8L>A0\$GH?BP\$NF0:n\$r\$9\$k\$N\$K\$O\$+\$J\$je(B
e\$BDq93\$,\$”\$j\$^\$9!#e(B

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

08/05/31 e\$B\$Ke(B Yukihiro M.[email protected]
e\$B\$5\$s\$O=q\$-\$^\$7\$?e(B:

|Integer#hex e\$B\$He(B #oct e\$B\$rDI2C\$9\$k\$N\$O\$I\$&\$G\$7\$g\$&\$+!#e(B
|String#hex e\$B\$?\$A\$NH?E>\$G\$9!#e(B
|
| 10000.hex #=> “2710”
|
|irb e\$B\$r7W;;5!Be\$o\$j\$K;H\$&>l9g\$KJXMx\$@\$H;W\$\$\$^\$9!#e(B
|10000.to_s(16) e\$B\$O\$A\$g\$C\$HD9\$9\$.\$G\$9\$7!#e(B

e\$BJXMx\$@\$H\$O;W\$\$\$^\$9\$,!“F1\$8L>A0\$GH?BP\$NF0:n\$r\$9\$k\$N\$K\$O\$+\$J\$je(B
e\$BDq93\$,\$”\$j\$^\$9!#e(B

hex e\$B\$H\$\$\$&%a%=%C%IL>\$J\$i!“e(Bhex e\$B\$^\$?\$Oe(B hex
e\$B\$N\$h\$&\$J\$b\$N\$,e(B
e\$BJV\$C\$F\$/\$k\$3\$H\$r4|BT\$7\$^\$9!#\$J\$N\$G!”\$I\$A\$i\$+\$H\$\$\$&\$He(B
String#hex e\$B\$NF0:n\$h\$je(B Integer#hex e\$B\$NF0:n\$NJ}\$,@5Ev\$@\$He(B
e\$B46\$8\$^\$9!#e(B

e\$B8_49@-\$NLdBj\$,\$"\$k\$N\$Ge(B String#hex e\$B\$rJQ99\$9\$k\$3\$H\$OL5M}e(B
e\$B\$@\$H;W\$\$\$^\$9\$,!"e(BInteger#hex e\$B\$N>c32\$K\$J\$k\$N\$O\$I\$&\$+\$He(B
e\$B;W\$\$\$^\$9!#e(B