Last active
April 29, 2021 23:52
-
-
Save leoarnold/cc75dd8d026c396ce2318231f8c1ddfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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