Class: Rulerr::Conversion
- Inherits:
-
Object
- Object
- Rulerr::Conversion
- Defined in:
- lib/rulerr.rb
Constant Summary collapse
- DEFAULT_UNIT =
'cm'
- FACTOR_BY_UNIT =
{ 'm' => 1.0, 'cm' => 100.0, 'mm' => 1000.0, 'px' => 3779.5275591, 'pt' => 2834.64567, 'in' => 39.37 }
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
-
#initialize(value) ⇒ Conversion
constructor
A new instance of Conversion.
- #to_cm ⇒ Object
- #to_in ⇒ Object
- #to_m ⇒ Object
-
#to_mm ⇒ Object
eg: “23.45cm” => “234.5mm”.
- #to_pt ⇒ Object
- #to_px ⇒ Object
Constructor Details
#initialize(value) ⇒ Conversion
Returns a new instance of Conversion.
17 18 19 20 21 22 23 |
# File 'lib/rulerr.rb', line 17 def initialize(value) @number = value.to_f @unit = ( value.match(/[a-z]{1,2}$/i).to_a[0] || DEFAULT_UNIT ).downcase end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
15 16 17 |
# File 'lib/rulerr.rb', line 15 def number @number end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
15 16 17 |
# File 'lib/rulerr.rb', line 15 def unit @unit end |
Instance Method Details
#to_cm ⇒ Object
30 31 32 |
# File 'lib/rulerr.rb', line 30 def to_cm to_m * FACTOR_BY_UNIT['cm'] end |
#to_in ⇒ Object
42 43 44 |
# File 'lib/rulerr.rb', line 42 def to_in to_m * FACTOR_BY_UNIT['in'] end |
#to_m ⇒ Object
34 35 36 |
# File 'lib/rulerr.rb', line 34 def to_m number / FACTOR_BY_UNIT[unit] end |
#to_mm ⇒ Object
eg: “23.45cm” => “234.5mm”
26 27 28 |
# File 'lib/rulerr.rb', line 26 def to_mm to_m * FACTOR_BY_UNIT['mm'] end |
#to_pt ⇒ Object
46 47 48 |
# File 'lib/rulerr.rb', line 46 def to_pt to_m * FACTOR_BY_UNIT['pt'] end |
#to_px ⇒ Object
38 39 40 |
# File 'lib/rulerr.rb', line 38 def to_px to_m * FACTOR_BY_UNIT['px'] end |