Created
December 13, 2019 05:34
-
-
Save stephancom/d10e811d34d765f7c97a4791ed79e19f to your computer and use it in GitHub Desktop.
feet/inches utilities
This file contains hidden or 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
# ___ _ _ | |
# |_ _|_ __ _ __ ___ _ _(_)__ _| | | |
# | || ' \| '_ \/ -_) '_| / _` | | | |
# |___|_|_|_| .__/\___|_| |_\__,_|_| | |
# |_| | |
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