Class: M26::Distance

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/m26_distance.rb

Constant Summary

Constants included from Constants

Constants::AUTHOR, Constants::DATE, Constants::EMAIL, Constants::KILOMETERS_PER_MILE, Constants::MILES_PER_KILOMETER, Constants::SECONDS_PER_HOUR, Constants::UOM_KILOMETERS, Constants::UOM_MILES, Constants::UOM_YARDS, Constants::VERSION, Constants::YARDS_PER_KILOMETER, Constants::YARDS_PER_MILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(v, um = UOM_MILES) ⇒ Distance

Returns a new instance of Distance.



15
16
17
18
# File 'lib/m26_distance.rb', line 15

def initialize(v, um=UOM_MILES)
  @value = v.to_f
  @uom   = um
end

Instance Attribute Details

#uomObject

Returns the value of attribute uom.



13
14
15
# File 'lib/m26_distance.rb', line 13

def uom
  @uom
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/m26_distance.rb', line 13

def value
  @value
end

Instance Method Details

#get_kilometersObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/m26_distance.rb', line 33

def get_kilometers
  case @uom
  when UOM_MILES
    return @value * KILOMETERS_PER_MILE
  when UOM_KILOMETERS
    return @value
  when UOM_YARDS
    return (@value / YARDS_PER_MILE) / MILES_PER_KILOMETER
  end
end

#get_milesObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/m26_distance.rb', line 22

def get_miles
  case @uom
    when UOM_MILES
      return @value
    when UOM_KILOMETERS
      return @value / KILOMETERS_PER_MILE
    when UOM_YARDS
      return @value / YARDS_PER_MILE
  end
end

#get_yardsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/m26_distance.rb', line 44

def get_yards
  case @uom
  when UOM_MILES
    return @value * YARDS_PER_MILE
  when UOM_KILOMETERS
    return (@value * MILES_PER_KILOMETER) * YARDS_PER_MILE
  when UOM_YARDS
    return @value
  end
end


76
77
78
# File 'lib/m26_distance.rb', line 76

def print_string
  return to_s << " #{get_miles()} #{get_kilometers()} #{get_yards()}"
end

#subtract(another_instance) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/m26_distance.rb', line 55

def subtract(another_instance)
  if (another_instance != nil)
    Distance.new(@value - another_instance.value, @uom)
  else
    nil
  end
end

#to_sObject



72
73
74
# File 'lib/m26_distance.rb', line 72

def to_s
  return "Distance: #{@value} #{@uom}"
end

#valid?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/m26_distance.rb', line 63

def valid?
  return false if @value == nil
  return false if @value < 0
  return true  if @uom == UOM_MILES
  return true  if @uom == UOM_KILOMETERS
  return true  if @uom == UOM_YARDS
  false
end