Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/casual_support/integer/to_hex.rb

Instance Method Summary collapse

Instance Method Details

#to_hex(width = 0) ⇒ String

Formats the Integer as a zero-padded lower-case hexadecimal string. If the length of the raw hexadecimal string exceeds the desired width, the string will be returned as-is (without padding or truncation).

Examples:

250.to_hex     # == "fa"
250.to_hex(4)  # == "00fa"
250.to_hex(1)  # == "fa"

Parameters:

Returns:



17
18
19
# File 'lib/casual_support/integer/to_hex.rb', line 17

def to_hex(width = 0)
  width > 1 ? self.to_s(16).rjust(width, "0") : self.to_s(16)
end