Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#hex

() -> ::Integer
hex → integer

Interprets the leading substring of self as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number; returns zero if there is no such leading substring:

Example
'0x0a'.hex        # => 10
'-1234'.hex       # => -4660
'0'.hex           # => 0
'non-numeric'.hex # => 0

Related: String#oct.

See also #hash #include? #gsub! #index also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0