Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created September 6, 2010 05:47
Show Gist options
  • Save mrkn/566692 to your computer and use it in GitHub Desktop.
Save mrkn/566692 to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'measure'
DTP = Measure::Context.new do
extend Measure::SIPrefixes
unit :meter, "m"
unit :inch, "in"
unit :didot_point, "pt"
alias_unit :cm, [:centi, :meter], "cm"
alias_unit :mm, [:milli, :meter], "mm"
alias_unit :m, :meter
alias_unit :pt, :didot_point
scale q(1).in, q(254).quo(100).cm
scale :inch, q(72).pt
const :a4_paper => {
:width => q(210).mm,
:height => q(297).mm,
}
end
DTP.a4_paper[:width] #=> 210 [mm]
DTP[1].in #=> 1 [in]
DTP[2].in.to_cm #=> (127/25) [cm]
DTP[1].cm + DTP[5].mm #=> (15/10) [cm]
DTP[5].mm + DTP[1].m #=> 15 [mm]
DTP.eval { q(1).in + q(144).pt } #=> 3 [in]
DTP.eval { q(1) } #=> 1
JPY = Measure::Context.new do
unit :円
prefix :千, 1_000
prefix :万, 10_000
prefix :億, 100_000_000
prefix :兆, 1_000_000_000_000
alias_unit :千円, [:千, :円], "千円"
alias_unit :万円, [:万, :円]
alias_unit :億円, [:億, :円]
alias_unit :兆円, [:兆, :円]
end
JPY[1].円 #=> 1 [円]
JPY[1].千円.to_円 #=> 1000 [円]
JPY[10].億円.to_千円 #=> 1000000 [千円]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment