tempture-convertor 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +55 -0
  3. data/lib/tempture_convertor.rb +86 -0
  4. metadata +95 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9ed8a313eed1f3b5d8943eccb7c202442f88edd325ab8a22887d05ca9eee425a
4
+ data.tar.gz: 3d30504a1ad9abd073f51a6782f2087f824a07d8ff59b1c309b64601521055d5
5
+ SHA512:
6
+ metadata.gz: 40e690dd017c1766c0e73e2d342f8368380de15994db96de871e77de60c228bf54f324784e536801f869861608b177f2073aa209aeb01ac2957424da20251d1b
7
+ data.tar.gz: a520c6496e9d983f7d5f47424142a5a70c6191a7972dabeaf2e3fa92ca20c51dddc17cb4797c3bda8f6216e96c0bab7d76d60252921223c1e4402cb41e72decf
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Temperature Convertor
2
+
3
+ convert temperature from fehrenheit or kelvin to celsius
4
+
5
+
6
+ ## Detail
7
+
8
+ The "Temperature" class stores and controls the value and convert function
9
+
10
+ **Constructor**
11
+
12
+ > ` def initialize(temperature, mode) `
13
+
14
+ The initialization process retrieve temperature user input and specific Temperature Mode and store temperature in celsius form.<br>
15
+ [param] temperature: float --> the temperature value<br>
16
+ [param] mode: TemperatureMode --> the temperature unit (Fahrenheit, Celsius or Kelvin)<br>
17
+ Note the TemperatureMode is an enumerate that holds value of TemperatureMode::Celsius, TemperatureMode::Fahrenheit or TemperatureMode::Kelvin. The implementation is:<br>
18
+
19
+ ```ruby
20
+ module TemperatureMode
21
+ Celsius = 1
22
+ Fahrenheit = 2
23
+ Kelvin = 4
24
+ end
25
+ ```
26
+
27
+ **method**
28
+
29
+ > ` def toFahrenheit() `
30
+
31
+ Convert the temperature to Fahrenheit<br>
32
+ [return] temperature in Fahrenheit, type in float<br>
33
+
34
+ > ` def toKelvin() `
35
+
36
+ Convert the temperature to Kelvin<br>
37
+ [return] temperature in Kelvin, type in float<br>
38
+
39
+ **Property**
40
+
41
+ > ` this.temperature `
42
+
43
+ temperature in Celsius, type in float
44
+
45
+ ## Get Start
46
+
47
+ Use following script for testing
48
+
49
+ ```ruby
50
+ temp = Temperature.new(-24, 1)
51
+
52
+ puts temp.toString
53
+ puts "Current temperature is #{'%.2f' % temp.toFahrenheit} °F"
54
+ puts "Current temperature is #{'%.2f' % temp.toKelvin} K"
55
+ ```
@@ -0,0 +1,86 @@
1
+ =begin
2
+ temperature convert program
3
+ read the temperature in celsius form
4
+ and get fahrenheit or kelvin form
5
+ =end
6
+
7
+ module TemperatureMode
8
+ Celsius = 1
9
+ Fahrenheit = 2
10
+ Kelvin = 4
11
+ end
12
+
13
+
14
+ class Temperature
15
+ # temperature class
16
+
17
+ def initialize(temperature, mode)
18
+ # initial temperature (load & save in celsius form)
19
+
20
+ # first check the mode
21
+ case mode
22
+ when TemperatureMode::Celsius
23
+ @temperature = temperature
24
+ when TemperatureMode::Fahrenheit
25
+ @temperature = Temperature.FahrenheitToCelsius(temperature)
26
+ when TemperatureMode::Kelvin
27
+ @temperature = Temperature.KelvinToCelsius(temperature)
28
+ else
29
+ # invalid mode met
30
+ raise ArgumentError, "The temperature can only be chosen within fehrenheit, celsius and kelvin!"
31
+ end
32
+
33
+ # now use switch case rather than if else
34
+ # if mode === TemperatureMode::Celsius
35
+ # @temperature = temperature
36
+ # elsif mode === TemperatureMode::Fahrenheit
37
+ # @temperature = Temperature.FahrenheitToCelsius(temperature)
38
+ # elsif mode === TemperatureMode::Kelvin
39
+ # @temperature = Temperature.KelvinToCelsius(temperature)
40
+ # else
41
+ # # invalid mode met
42
+ # raise ArgumentError, "The temperature can only be chosen within fehrenheit, celsius and kelvin!"
43
+ # end
44
+
45
+ end
46
+
47
+ attr_reader :temperature
48
+
49
+ def toFahrenheit()
50
+ # °F = °C * 9/5 + 32
51
+ return @temperature * 9.0 / 5.0 + 32.0
52
+ end
53
+
54
+ def toKelvin()
55
+ # K = °C + 273.15
56
+ return @temperature + 273.15
57
+ end
58
+
59
+ def self.FahrenheitToCelsius(fahrenheit)
60
+ # °C = (°F - 32) ÷ (9/5)
61
+ return (fahrenheit - 32.0) / 5.0 * 9.0
62
+ end
63
+
64
+ def self.CensiusToFahrenheit(celsius)
65
+ # °F = °C * 9/5 + 32
66
+ return celsius * 9.0 / 5.0 + 32.0
67
+ end
68
+
69
+ def self.KelvinToCelsius(kelvin)
70
+ return kelvin - 273.15
71
+ end
72
+
73
+ def toString
74
+ return "Now, It's #{@temperature} ℃"
75
+ end
76
+
77
+ end
78
+
79
+ if __FILE__ == $0
80
+ temp = Temperature.new(-24, 1)
81
+
82
+ puts temp.toString
83
+ # puts temp.temperature
84
+ puts "Current temperature is #{'%.2f' % temp.toFahrenheit} °F"
85
+ puts "Current temperature is #{'%.2f' % temp.toKelvin} K"
86
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tempture-convertor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sicheng Zhong
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: |-
13
+ # Temperature Convertor
14
+
15
+ convert temperature from fehrenheit or kelvin to celsius
16
+
17
+
18
+ ## Detail
19
+
20
+ The "Temperature" class stores and controls the value and convert function
21
+
22
+ **Constructor**
23
+
24
+ > ` def initialize(temperature, mode) `
25
+
26
+ The initialization process retrieve temperature user input and specific Temperature Mode and store temperature in celsius form.<br>
27
+ [param] temperature: float --> the temperature value<br>
28
+ [param] mode: TemperatureMode --> the temperature unit (Fahrenheit, Celsius or Kelvin)<br>
29
+ Note the TemperatureMode is an enumerate that holds value of TemperatureMode::Celsius, TemperatureMode::Fahrenheit or TemperatureMode::Kelvin. The implementation is:<br>
30
+
31
+ ```ruby
32
+ module TemperatureMode
33
+ Celsius = 1
34
+ Fahrenheit = 2
35
+ Kelvin = 4
36
+ end
37
+ ```
38
+
39
+ **method**
40
+
41
+ > ` def toFahrenheit() `
42
+
43
+ Convert the temperature to Fahrenheit<br>
44
+ [return] temperature in Fahrenheit, type in float<br>
45
+
46
+ > ` def toKelvin() `
47
+
48
+ Convert the temperature to Kelvin<br>
49
+ [return] temperature in Kelvin, type in float<br>
50
+
51
+ **Property**
52
+
53
+ > ` this.temperature `
54
+
55
+ temperature in Celsius, type in float
56
+
57
+ ## Get Start
58
+
59
+ Use following script for testing
60
+
61
+ ```ruby
62
+ temp = Temperature.new(-24, 1)
63
+
64
+ puts temp.toString
65
+ puts "Current temperature is #{'%.2f' % temp.toFahrenheit} °F"
66
+ puts "Current temperature is #{'%.2f' % temp.toKelvin} K"
67
+ ```
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - README.md
73
+ - lib/tempture_convertor.rb
74
+ homepage: https://github.com/Ayano-Keiko
75
+ licenses:
76
+ - GPL-3.0-only
77
+ metadata: {}
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 4.0.3
93
+ specification_version: 4
94
+ summary: temperature converting between fehrenheit, kelvin and celsius
95
+ test_files: []