Class: Languages::Generic::Millimetres
- Inherits:
-
Object
- Object
- Languages::Generic::Millimetres
- Defined in:
- lib/languages/generic/millimetres.rb
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(int) ⇒ Millimetres
constructor
A new instance of Millimetres.
- #to_dots(ratio = :dpi203) ⇒ Object
- #to_i ⇒ Object
Constructor Details
#initialize(int) ⇒ Millimetres
Returns a new instance of Millimetres.
4 5 6 |
# File 'lib/languages/generic/millimetres.rb', line 4 def initialize(int) @int = int end |
Instance Method Details
#+(other) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/languages/generic/millimetres.rb', line 12 def +(other) if other.is_a? Millimetres self.class.new(@int + other.to_i) elsif other.is_a? Integer self.class.new(@int + other) else if other.respond_to? :coerce a,b = other.coerce(self) a + b else raise TypeError, "#{other.class} can't be coerced into #{self.class.name}" end end end |
#-(other) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/languages/generic/millimetres.rb', line 27 def -(other) if other.is_a? Millimetres self.class.new(@int - other.to_i) elsif other.is_a? Integer self.class.new(@int - other) else if other.respond_to? :coerce a,b = other.coerce(self) a - b else raise TypeError, "#{other.class} can't be coerced into #{self.class.name}" end end end |
#==(other) ⇒ Object
42 43 44 |
# File 'lib/languages/generic/millimetres.rb', line 42 def ==(other) @int == other.to_i end |
#coerce(other) ⇒ Object
46 47 48 |
# File 'lib/languages/generic/millimetres.rb', line 46 def coerce(other) [Millimetres.new(other),self] end |
#to_dots(ratio = :dpi203) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/languages/generic/millimetres.rb', line 50 def to_dots(ratio = :dpi203) dpm = if ratio == :dpi203 (203/25.4) else (300/25.4) end Languages::Generic::Dots.new((@int * dpm).round) end |
#to_i ⇒ Object
8 9 10 |
# File 'lib/languages/generic/millimetres.rb', line 8 def to_i @int end |