Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created October 30, 2022 23:41
Show Gist options
  • Save Snarp/d4b617c87fd5460138dba0a38b5746e4 to your computer and use it in GitHub Desktop.
Save Snarp/d4b617c87fd5460138dba0a38b5746e4 to your computer and use it in GitHub Desktop.
Converts ounces per linear yard (a common measurement of fabric density in North America) to GSM (grams per square meter)
# Converts ounces per linear yard (a common measurement of fabric density in
# North America) to GSM (grams per square meter).
#
# When buying fabric, a "linear yard" refers to a yard-long piece of fabric cut
# from a bolt. Ounces per linear yard are calculated by weighing a bolt of
# fabric, then dividing its weight by its length in yards when fully unspooled.
# Fabric bolts may be of varying heights, but are generally more than 1 yard
# tall; 4-5 feet is a common height.
#
# (Q) Given this variability in bolt size, why do so many vendors describe
# fabric weights using oz/linear yd, rather than converting to a more-useful
# measure like oz/sq yd?
#
# (A) Well, you see, {{DMCA TAKEDOWN}}
#
# @param [Numeric] oz_p_linear_yd Weight of 1 yd fabric cut from bolt
# @param [Numeric] width_in_yd Height of fabric bolt in question
# @return [Float] Grams per square meter
def oz_per_linear_yd_to_gsm(oz_p_linear_yd=8.5, width_in_yd=(60.0/36))
width_in_m = yd_to_m(width_in_yd)
area_in_m = yd_to_m(1.0) * width_in_m
weight_in_g = oz_to_g(oz_p_linear_yd)
return weight_in_g.to_f / area_in_m.to_f
end
alias :oly_to_gsm :oz_per_linear_yd_to_gsm
# Ounces per linear yard => Ounces per square yard
#
# @param [Numeric] oz_p_linear_yd Ounces per linear yard
# @param [Numeric] width_in_in Height of fabric bolt in question
# @return [Float] Ounces per square yard (oz/yd²)
def oz_p_linear_yd_to_oz_p_sqyd(oz_p_linear_yd=8.5, width_in_in=60)
width_in_yd = in_to_yd(width_in_in)
return oz_p_linear_yd / width_in_yd
end
alias :oly_to_osy :oz_p_linear_yd_to_oz_p_sqyd
# Ounces per square yard => Grams per square meter
#
# @param [Numeric] oz_p_sqyd Ounces per square yard (oz/yd²)
# @return [Float] Grams per square meter (GSM or g/m²)
def oz_p_sqyd_to_g_p_sqm(oz_p_sqyd=5.1)
weight_in_g = oz_to_g(oz_p_sqyd)
width_in_m = yd_to_m(1.0)
area_in_sqm = width_in_m * width_in_m
return weight_in_g / area_in_sqm
end
alias :osy_to_gsm :oz_p_sqyd_to_g_p_sqm
# HELPERS
VALUES = {
m_per_yd: 0.9144,
g_per_oz: 28.34952,
in_per_yd: 36.0,
}
def yd_to_m(val)
val * VALUES[:m_per_yd]
end
def m_to_yd(val)
val / VALUES[:m_per_yd]
end
def oz_to_g(val)
val * VALUES[:g_per_oz]
end
def g_to_oz(val)
val / VALUES[:g_per_oz]
end
def yd_to_in(val)
val * VALUES[:in_per_yd]
end
def in_to_yd(val)
val / VALUES[:in_per_yd]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment