Skip to content

Instantly share code, notes, and snippets.

@leoarnold
Last active April 29, 2021 23:52
Show Gist options
  • Save leoarnold/cc75dd8d026c396ce2318231f8c1ddfa to your computer and use it in GitHub Desktop.
Save leoarnold/cc75dd8d026c396ce2318231f8c1ddfa to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class Integer
def to_bin_s
bin = to_s(2)
bin.prepend('0') until (bin.length % 8).zero?
bin.scan(/.{4}/).join(' ')
end
def to_hex_s
hex = to_s(16)
hex.prepend('0') until hex.length.even?
hex.scan(/.{2}/).join(' ')
end
def to_oct_s
to_s(8)
end
def inspect
"#{self} (HEX: #{to_hex_s}, OCT: #{to_oct_s}, BIN: #{to_bin_s})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment