Skip to content

Instantly share code, notes, and snippets.

@flanger001
Created January 11, 2024 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flanger001/65e380a322f9af9ade5fdb33325856aa to your computer and use it in GitHub Desktop.
Save flanger001/65e380a322f9af9ade5fdb33325856aa to your computer and use it in GitHub Desktop.
Binary nonsense
# frozen_string_literal: true
module IntegerExtensions
refine Integer do
def as_binary_string(width = nil)
width ||= self.bit_length
"%0#{width}b" % self
end
def as_byte
as_binary_string(8)
end
end
end
3.as_binary_string
=> "11"
3.as_binary_string(3)
=> "011"
255.as_byte
=> "11111111"
233.byte
=> "11101001"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment