Skip to content

Commit 88dc3c4

Browse files
[DOC] More tweaks for String#hex
1 parent 8836f26 commit 88dc3c4

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

string.c

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10728,19 +10728,44 @@ rb_str_scan(VALUE str, VALUE pat)
1072810728
* call-seq:
1072910729
* hex -> integer
1073010730
*
10731-
* Interprets the leading substring of +self+ as hexadecimal;
10732-
* returns its integer value:
10731+
* Interprets the leading substring of +self+ as hexadecimal, possibly signed;
10732+
* returns its value as an integer.
1073310733
*
10734-
* '0xFFFF'.hex # => 65535
10735-
* 'FFzzzFF'.hex # => 255 # Hex ends at first non-hex character, 'z'.
10736-
* 'ffzzzFF'.hex # => 255 # Case does not matter.
10737-
* '-FFzzzFF'.hex # => -255 # May have leading '-'.
10738-
* '0xFFzzzFF'.hex # => 255 # May have leading '0x'.
10739-
* '-0xFFzzzFF'.hex # => -255 # May have leading '-0x'.
10734+
* The leading substring is interpreted as hexadecimal when it begins with:
1074010735
*
10741-
* Returns zero if there is no such leading substring:
10736+
* - One or more character representing hexadecimal digits
10737+
* (each in one of the ranges <tt>'0'..'9'</tt>, <tt>'a'..'f'</tt>, or <tt>'A'..'F'</tt>);
10738+
* the string to be interpreted ends at the first character that does not represent a hexadecimal digit:
10739+
*
10740+
* 'f'.hex # => 15
10741+
* '11'.hex # => 17
10742+
* 'FFF'.hex # => 4095
10743+
* 'fffg'.hex # => 4095
10744+
* 'foo'.hex # => 15 # 'f' hexadecimal, 'oo' not.
10745+
* 'bar'.hex # => 186 # 'ba' hexadecimal, 'r' not.
10746+
* 'deadbeef'.hex # => 3735928559
10747+
*
10748+
* - <tt>'0x'</tt> or <tt>'0X'</tt>, followed by one or more hexadecimal digits:
10749+
*
10750+
* '0xfff'.hex # => 4095
10751+
* '0xfffg'.hex # => 4095
10752+
*
10753+
* Any of the above may prefixed with <tt>'-'</tt>, which negates the interpreted value:
10754+
*
10755+
* '-fff'.hex # => -4095
10756+
* '-0xFFF'.hex # => -4095
10757+
*
10758+
* For any substring not described above, returns zero:
10759+
*
10760+
* 'xxx'.hex # => 0
10761+
* ''.hex # => 0
10762+
*
10763+
* Note that, unlike #oct, this method interprets only hexadecimal,
10764+
* and not binary, octal, or decimal notations:
1074210765
*
10743-
* 'zzz'.hex # => 0
10766+
* '0b111'.hex # => 45329
10767+
* '0o777'.hex # => 0
10768+
* '0d999'.hex # => 55705
1074410769
*
1074510770
* Related: See {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].
1074610771
*/

0 commit comments

Comments
 (0)