Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created December 13, 2019 05:34
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 stephancom/d10e811d34d765f7c97a4791ed79e19f to your computer and use it in GitHub Desktop.
Save stephancom/d10e811d34d765f7c97a4791ed79e19f to your computer and use it in GitHub Desktop.
feet/inches utilities
# ___ _ _
# |_ _|_ __ _ __ ___ _ _(_)__ _| |
# | || ' \| '_ \/ -_) '_| / _` | |
# |___|_|_|_| .__/\___|_| |_\__,_|_|
# |_|
module Imperial
extend ActiveSupport::Concern
included do
# options for select menu
FEET_RANGE = (4..6).freeze
INCHES_RANGE = (0..11).freeze
MM_PER_INCH = 25.4
end
class_methods do
def feet_inches
FEET_RANGE.to_a.product(INCHES_RANGE.to_a)
end
def height_mm_options
feet_inches.map { |feet, inch|
[feet_inches_to_s(feet, inch),
feet_inches_to_mm(feet, inch)]
}
end
def height_inch_options
feet_inches.map { |feet, inch|
[feet_inches_to_s(feet, inch),
feet * 12 + inch]
}
end
def feet_inches_to_s(feet, inches)
"#{feet}' #{inches}''"
end
def feet_inches_to_mm(feet, inches)
(feet * 12 + inches) * 25.4
end
def mm_to_inches(mm)
(mm / MM_PER_INCH).round
end
def inches_to_mm(inches)
(inches * MM_PER_INCH).round
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment