-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_conversion.rb
49 lines (38 loc) · 966 Bytes
/
task_conversion.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class LengthConverter
attr_reader :length, :from_unit, :to_unit
UNITS_METER = :meter
UNITS_FEET = :feet
UNITS_INCH = :inch
UNITS_LENGTH = {
UNITS_METER => 1.0,
UNITS_FEET => 3.28,
UNITS_INCH => 39.37
}
def initialize(length:, from_unit:, to_unit:)
@length = length
@from_unit = from_unit
@to_unit = to_unit
end
def convert
from = UNITS_LENGTH[from_unit]
to = UNITS_LENGTH[to_unit]
if from == nil
p "Error: 変換元単位が存在しません。from_unit: #{from_unit}"
return
end
if to == nil
p "Error: 変換後単位が存在しません。to_unit: #{to_unit}"
return
end
puts "from: #{from}"
puts "to: #{to}"
(length / from*to).round(2)
end
end
length_converter = LengthConverter.new(
length: 1,
from_unit: LengthConverter::UNITS_METER,
to_unit: LengthConverter::UNITS_FEET
)
converted_length = length_converter.convert
p converted_length