basic_temperature 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +33 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +12 -0
- data/.travis.yml +2 -1
- data/README.md +223 -9
- data/basic_temperature.gemspec +6 -1
- data/bin/console +4 -4
- data/docs/apple-touch-icon.png +0 -0
- data/docs/classes/BasicTemperature.html +1227 -0
- data/docs/classes/BasicTemperature/InitializationArgumentsError.html +94 -0
- data/docs/classes/BasicTemperature/InvalidDegreesError.html +94 -0
- data/docs/classes/BasicTemperature/InvalidNumericOrTemperatureError.html +94 -0
- data/docs/classes/BasicTemperature/InvalidScaleError.html +94 -0
- data/docs/classes/Object.html +274 -0
- data/docs/classes/Temperature.html +1227 -0
- data/docs/created.rid +4 -0
- data/docs/css/github.css +123 -0
- data/docs/css/main.css +374 -0
- data/docs/css/panel.css +361 -0
- data/docs/css/reset.css +48 -0
- data/docs/favicon.ico +0 -0
- data/docs/files/lib/basic_temperature/temperature_rb.html +73 -0
- data/docs/files/lib/basic_temperature/version_rb.html +84 -0
- data/docs/files/lib/basic_temperature_rb.html +112 -0
- data/docs/i/arrow-down.svg +8 -0
- data/docs/i/arrow-right.svg +8 -0
- data/docs/i/search.svg +12 -0
- data/docs/i/tree_bg.svg +8 -0
- data/docs/index.html +11 -0
- data/docs/js/highlight.pack.js +1 -0
- data/docs/js/jquery-1.3.2.min.js +19 -0
- data/docs/js/main.js +25 -0
- data/docs/js/navigation.js +105 -0
- data/docs/js/navigation.js.gz +0 -0
- data/docs/js/search_index.js +1 -0
- data/docs/js/search_index.js.gz +0 -0
- data/docs/js/searchdoc.js +465 -0
- data/docs/js/searcher.js +229 -0
- data/docs/js/searcher.js.gz +0 -0
- data/docs/panel/index.html +47 -0
- data/docs/panel/links.html +12 -0
- data/docs/panel/tree.js +1 -0
- data/lib/basic_temperature.rb +552 -75
- data/lib/basic_temperature/temperature.rb +5 -0
- data/lib/basic_temperature/version.rb +1 -1
- data/logo.png +0 -0
- metadata +112 -4
- data/Gemfile.lock +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 306e98f11732ad722fb25ecf4cbdfbe8b0c1151e34ddffa2fa202656fd16d2de
|
4
|
+
data.tar.gz: 1f287528c4653c3f270ad8ec6d27afd2ee7e097b9b1b9a4dc216daefb9b7caa4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07a9da936a9b9e282423b91993f09255ddd8783412477d77d412401b6f7b110cada000a95dea742725a0584ab3852f3760691d6dc714a528af5a51a3224290ea
|
7
|
+
data.tar.gz: 25833d006ce3d8d000ecd0ee30ebe80e66acbbbe4f9c2fecbea6fb44732057e240a6cddd44fa2acf23f7b32c313b0a0f95a0a660c0f7b2a37fe20cd1c37f7ade
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# DOCS: https://docs.codeclimate.com/docs/advanced-configuration
|
2
|
+
version: 2
|
3
|
+
|
4
|
+
plugins:
|
5
|
+
rubocop:
|
6
|
+
enabled: true
|
7
|
+
channel: rubocop-0-80
|
8
|
+
|
9
|
+
exclude_patterns:
|
10
|
+
- spec/**/*
|
11
|
+
|
12
|
+
checks:
|
13
|
+
argument-count:
|
14
|
+
enabled: false
|
15
|
+
complex-logic:
|
16
|
+
enabled: false
|
17
|
+
file-lines:
|
18
|
+
enabled: false
|
19
|
+
method-complexity:
|
20
|
+
enabled: false
|
21
|
+
method-count:
|
22
|
+
enabled: false
|
23
|
+
method-lines:
|
24
|
+
enabled: false
|
25
|
+
nested-control-flow:
|
26
|
+
enabled: false
|
27
|
+
return-statements:
|
28
|
+
enabled: false
|
29
|
+
similar-code:
|
30
|
+
enabled: false
|
31
|
+
identical-code:
|
32
|
+
enabled: false
|
33
|
+
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
---
|
2
|
+
# DOCS: https://docs.travis-ci.com/user/languages/ruby/#bundler-20
|
2
3
|
language: ruby
|
3
4
|
|
4
5
|
cache: bundler
|
5
6
|
|
6
7
|
before_install:
|
8
|
+
# Bundler 2.0
|
7
9
|
- yes | gem update --system --force
|
8
10
|
- gem install bundler -v 2.1.2
|
9
11
|
|
@@ -15,5 +17,4 @@ rvm:
|
|
15
17
|
- 2.7.0
|
16
18
|
|
17
19
|
script:
|
18
|
-
- bundle exec rubocop
|
19
20
|
- bundle exec rspec
|
data/README.md
CHANGED
@@ -1,8 +1,30 @@
|
|
1
|
-
|
1
|
+
## Basic Temperature ##
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/basic_temperature.svg)](https://rubygems.org/gems/basic_temperature) [![Build Status](https://travis-ci.com/marian13/basic_temperature.svg?branch=master)](https://travis-ci.com/marian13/basic_temperature) [![Maintainability](https://api.codeclimate.com/v1/badges/21dc5d50cf5de8346a3c/maintainability)](https://codeclimate.com/github/marian13/basic_temperature/maintainability) [![Coverage Status](https://coveralls.io/repos/github/marian13/basic_temperature/badge.svg)](https://coveralls.io/github/marian13/basic_temperature) [![Inline docs](http://inch-ci.org/github/marian13/basic_temperature.svg?branch=master)](http://inch-ci.org/github/marian13/basic_temperature)
|
4
|
+
[![Patreon](https://img.shields.io/badge/patreon-donate-orange.svg)](https://www.patreon.com/user?u=31435716&fan_landing=true)
|
5
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
4
6
|
|
5
|
-
|
7
|
+
![alt text](https://raw.githubusercontent.com/marian13/basic_temperature/master/logo.png)
|
8
|
+
|
9
|
+
`basic_temperature` is a Ruby library which provides a simple [value object](https://martinfowler.com/bliki/ValueObject.html) to work with temperatures and
|
10
|
+
allows to perform basic operations like conversion from Celsius to Kelvin, from Kelvin to Fahrenheit etc.
|
11
|
+
|
12
|
+
|
13
|
+
### Features
|
14
|
+
- Provides a `BasicTemperature` class which encapsulates all information about a certain temperature, such
|
15
|
+
as its amount of degrees and its scale.
|
16
|
+
- Provides APIs for exchanging temperatures from one scale to another (currently Celsius, Fahrenheit, Kelvin and
|
17
|
+
Rankine).
|
18
|
+
|
19
|
+
- Allows comparing temperatures between each other.
|
20
|
+
- Supports basic math operations like addition and subtraction.
|
21
|
+
- Tested against Ruby 2.3, 2.4, 2.5, 2.6 & 2.7. See
|
22
|
+
[.travis-ci.yml](https://github.com/marian13/basic_temperature/blob/9b13cb9909b57c51bb5dc05a8989d07a314e67d6/.travis.yml)
|
23
|
+
for the exact versions.
|
24
|
+
|
25
|
+
### Dependecies
|
26
|
+
|
27
|
+
* None.
|
6
28
|
|
7
29
|
## Installation
|
8
30
|
|
@@ -12,6 +34,14 @@ Add this line to your application's Gemfile:
|
|
12
34
|
gem 'basic_temperature'
|
13
35
|
```
|
14
36
|
|
37
|
+
#### Warning
|
38
|
+
|
39
|
+
Since this gem is under active development and there are often backward-incompatible changes between minor releases, you might want to use a conservative version lock in your Gemfile:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
gem 'basic_temperature', '~> 0.2.1'
|
43
|
+
```
|
44
|
+
|
15
45
|
And then execute:
|
16
46
|
|
17
47
|
$ bundle install
|
@@ -22,19 +52,203 @@ Or install it yourself as:
|
|
22
52
|
|
23
53
|
## Usage
|
24
54
|
|
25
|
-
|
55
|
+
### Creating Temperatures
|
56
|
+
|
57
|
+
A new temperature can be created in multiple ways:
|
58
|
+
|
59
|
+
- Using keyword arguments:
|
26
60
|
|
27
|
-
|
61
|
+
```ruby
|
62
|
+
Temperature.new(degrees: 0, scale: :celsius)
|
63
|
+
```
|
28
64
|
|
29
|
-
|
65
|
+
- Using positional arguments:
|
30
66
|
|
31
|
-
|
67
|
+
```ruby
|
68
|
+
Temperature.new(0, :celsius)
|
69
|
+
```
|
32
70
|
|
33
|
-
|
71
|
+
- Even more concise way using `Temperature.[]` (an alias of `Temperature.new`):
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Temperature[0, :celsius]
|
75
|
+
```
|
76
|
+
|
77
|
+
### Creating Temperatures from already existing temperature objects
|
78
|
+
|
79
|
+
Sometimes it is useful to create a new temperature from already existing one.
|
80
|
+
|
81
|
+
For such cases, there are `set_degrees` and `set_scale`.
|
82
|
+
|
83
|
+
Since temperatures are [value objects](https://martinfowler.com/bliki/ValueObject.html), both methods returns
|
84
|
+
new instances.
|
85
|
+
|
86
|
+
Examples:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
temperature = Temperature[0, :celsius]
|
90
|
+
# => 0 °C
|
91
|
+
|
92
|
+
new_temperature = temperature.set_degrees(15)
|
93
|
+
# => 15 °C
|
94
|
+
|
95
|
+
temperature = Temperature[0, :celsius]
|
96
|
+
# => 0 °C
|
97
|
+
|
98
|
+
new_temperature = temperature.set_scale(:kelvin)
|
99
|
+
# => 0 K
|
100
|
+
```
|
101
|
+
|
102
|
+
### Conversions
|
103
|
+
|
104
|
+
Temperatures can be converted to diffirent scales.
|
105
|
+
|
106
|
+
Currently, the following scales are supported: `Celsius`, `Fahrenheit`, `Kelvin` and `Rankine`.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
Temperature[20, :celsius].to_celsius
|
110
|
+
# => 20 °C
|
111
|
+
|
112
|
+
Temperature[20, :celsius].to_fahrenheit
|
113
|
+
# => 68 °F
|
114
|
+
|
115
|
+
Temperature[20, :celsius].to_kelvin
|
116
|
+
# => 293.15 K
|
117
|
+
|
118
|
+
Temperature[20, :celsius].to_rankine
|
119
|
+
# => 527.67 °R
|
120
|
+
```
|
121
|
+
|
122
|
+
If it is necessary to convert scale dynamically, `to_scale` method is available.
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
Temperature[20, :celsius].to_scale(scale)
|
126
|
+
```
|
127
|
+
|
128
|
+
All conversion formulas are taken from
|
129
|
+
[RapidTables](https://www.rapidtables.com/convert/temperature/index.html).
|
130
|
+
|
131
|
+
Conversion precision: 2 accurate digits after the decimal dot.
|
132
|
+
|
133
|
+
### Comparison
|
134
|
+
|
135
|
+
Temperature implements idiomatic [<=> spaceship operator](https://ruby-doc.org/core/Comparable.html) and mixes in [Comparable](https://ruby-doc.org/core/Comparable.html) module.
|
136
|
+
|
137
|
+
As a result, all methods from [Comparable](https://ruby-doc.org/core/Comparable.html) are available, e.g:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
Temperature[20, :celsius] < Temperature[25, :celsius]
|
141
|
+
# => true
|
34
142
|
|
35
|
-
|
143
|
+
Temperature[20, :celsius] <= Temperature[25, :celsius]
|
144
|
+
# => true
|
36
145
|
|
146
|
+
Temperature[20, :celsius] == Temperature[25, :celsius]
|
147
|
+
# => false
|
148
|
+
|
149
|
+
Temperature[20, :celsius] > Temperature[25, :celsius]
|
150
|
+
# => false
|
151
|
+
|
152
|
+
Temperature[20, :celsius] >= Temperature[25, :celsius]
|
153
|
+
# => false
|
154
|
+
|
155
|
+
Temperature[20, :celsius].between?(Temperature[15, :celsius], Temperature[25, :celsius])
|
156
|
+
# => true
|
157
|
+
|
158
|
+
# Starting from Ruby 2.4.6
|
159
|
+
Temperature[20, :celsius].clamp(Temperature[20, :celsius], Temperature[25, :celsius])
|
160
|
+
# => 20 °C
|
161
|
+
```
|
162
|
+
|
163
|
+
Please note, if the second temperature has a different scale, the first temperature is automatically
|
164
|
+
converted to that scale before comparison.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
Temperature[20, :celsius] == Temperature[293.15, :kelvin]
|
168
|
+
# => true
|
169
|
+
```
|
170
|
+
|
171
|
+
#### IMPORTANT !!!
|
172
|
+
|
173
|
+
`degrees` are rounded to the nearest value with a precision of 2 decimal digits before comparison.
|
174
|
+
|
175
|
+
This means the following temperatures are considered as equal:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
Temperature[20.020, :celsius] == Temperature[20.024, :celsius]
|
179
|
+
# => true
|
180
|
+
|
181
|
+
Temperature[20.025, :celsius] == Temperature[20.029, :celsius]
|
182
|
+
# => true
|
183
|
+
```
|
184
|
+
|
185
|
+
while these ones are treated as NOT equal:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
Temperature[20.024, :celsius] == Temperature[20.029, :celsius]
|
189
|
+
# => false
|
190
|
+
```
|
191
|
+
|
192
|
+
### Math
|
193
|
+
|
194
|
+
#### Addition/Subtraction.
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
Temperature[20, :celsius] + Temperature[10, :celsius]
|
198
|
+
# => 30 °C
|
199
|
+
|
200
|
+
Temperature[20, :celsius] - Temperature[10, :celsius]
|
201
|
+
# => 10 °C
|
202
|
+
```
|
203
|
+
|
204
|
+
If second temperature has a different scale, first temperature is automatically converted to that scale
|
205
|
+
before `degrees` addition/subtraction.
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
Temperature[283.15, :kelvin] + Temperature[10, :celsius]
|
209
|
+
# => 10 °C
|
210
|
+
```
|
211
|
+
|
212
|
+
Returned temperature will have the same scale as the second temperature.
|
213
|
+
|
214
|
+
It is possible to add/subtract numerics.
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
Temperature[20, :celsius] + 10
|
218
|
+
# => 30 °C
|
219
|
+
|
220
|
+
Temperature[20, :celsius] - 10
|
221
|
+
# => 10 °C
|
222
|
+
```
|
223
|
+
|
224
|
+
In such cases, returned temperature will have the same scale as the first temperature.
|
225
|
+
|
226
|
+
Also [Ruby coersion mechanism](https://ruby-doc.org/core/Numeric.html#method-i-coerce) is supported.
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
10 + Temperature[20, :celsius]
|
230
|
+
# => 30 °C
|
231
|
+
|
232
|
+
10 - Temperature[20, :celsius]
|
233
|
+
# => -10 °C
|
234
|
+
```
|
235
|
+
|
236
|
+
#### Negation
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
-Temperature[20, :celsius]
|
240
|
+
# => -20 °C
|
241
|
+
```
|
242
|
+
|
243
|
+
## Versioning
|
244
|
+
Basic Temperature follows the [Semantic Versioning](https://semver.org/) standard.
|
245
|
+
|
246
|
+
## Contributing
|
247
|
+
|
248
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marian13/basic_temperature.
|
37
249
|
|
38
250
|
## License
|
39
251
|
|
40
252
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
253
|
+
|
254
|
+
Copyright (c) 2020 [Marian Kostyk](http://mariankostyk.com).
|
data/basic_temperature.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.summary = 'Value object for basic temperature operations.'
|
13
13
|
spec.description =
|
14
14
|
'Value object for basic temperature operations like ' \
|
15
|
-
'conversions from
|
15
|
+
'conversions from Celsius to Fahrenhait or Kelvin etc.'
|
16
16
|
|
17
17
|
spec.homepage = 'https://github.com/marian13/basic_temperature'
|
18
18
|
|
@@ -33,7 +33,12 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.require_paths = ['lib']
|
34
34
|
|
35
35
|
spec.add_development_dependency 'byebug', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'coveralls'
|
36
37
|
spec.add_development_dependency 'rake', '~> 12.0'
|
38
|
+
spec.add_development_dependency 'rerun'
|
39
|
+
spec.add_development_dependency 'reverse_coverage'
|
37
40
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
38
41
|
spec.add_development_dependency 'rubocop', '~> 0.80.0'
|
42
|
+
spec.add_development_dependency 'sdoc'
|
43
|
+
spec.add_development_dependency 'simplecov'
|
39
44
|
end
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'basic_temperature/temperature'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require
|
10
|
+
# require 'pry'
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start(__FILE__)
|
Binary file
|
@@ -0,0 +1,1227 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
<head>
|
4
|
+
<title>BasicTemperature</title>
|
5
|
+
<meta charset="UTF-8" />
|
6
|
+
<link rel="stylesheet" href="../css/reset.css" type="text/css" media="screen" />
|
7
|
+
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" />
|
8
|
+
<link rel="stylesheet" href="../css/github.css" type="text/css" media="screen" />
|
9
|
+
<script src="../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
|
10
|
+
<script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
|
11
|
+
<script src="../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>
|
12
|
+
|
13
|
+
|
14
|
+
<meta property="og:title" value="BasicTemperature">
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
<meta name="description" content="Temperature is a simple Value Object for basic temperature operations like conversions from Celsius to Fahrenhait or Kelvin etc. Supported scales: Celsius, Fahrenheit, Kelvin and Rankine.">
|
19
|
+
<meta property="og:description" content="Temperature is a simple Value Object for basic temperature operations like conversions from Celsius to Fahrenhait or Kelvin etc. Supported scales: Celsius, Fahrenheit, Kelvin and Rankine.">
|
20
|
+
|
21
|
+
|
22
|
+
<meta name="keywords" content="BasicTemperature class">
|
23
|
+
|
24
|
+
<meta name="keywords" content="[], new, set_degrees, set_scale, to_scale, to_celsius, to_fahrenheit, to_kelvin, to_rankine, <=>, +, -, -@">
|
25
|
+
|
26
|
+
</head>
|
27
|
+
|
28
|
+
<body>
|
29
|
+
<div class="banner">
|
30
|
+
|
31
|
+
<h1>
|
32
|
+
<span class="type">Class</span>
|
33
|
+
BasicTemperature
|
34
|
+
|
35
|
+
<span class="parent"><
|
36
|
+
|
37
|
+
<a href="Object.html">Object</a>
|
38
|
+
|
39
|
+
</span>
|
40
|
+
|
41
|
+
</h1>
|
42
|
+
<ul class="files">
|
43
|
+
|
44
|
+
<li><a href="../files/lib/basic_temperature_rb.html">lib/basic_temperature.rb</a></li>
|
45
|
+
|
46
|
+
<li><a href="../files/lib/basic_temperature/version_rb.html">lib/basic_temperature/version.rb</a></li>
|
47
|
+
|
48
|
+
</ul>
|
49
|
+
</div>
|
50
|
+
<div id="bodyContent">
|
51
|
+
<div id="content">
|
52
|
+
|
53
|
+
<div class="description">
|
54
|
+
|
55
|
+
<p><a href="BasicTemperature.html"><code>Temperature</code></a> is a simple <a href="https://martinfowler.com/bliki/ValueObject.html">Value Object</a> for basic temperature operations like conversions from <code>Celsius</code> to <code>Fahrenhait</code> or <code>Kelvin</code> etc.</p>
|
56
|
+
|
57
|
+
<p>Supported scales: <code>Celsius</code>, <code>Fahrenheit</code>, <code>Kelvin</code> and <code>Rankine</code>.</p>
|
58
|
+
|
59
|
+
<h2 id="class-BasicTemperature-label-Creating+Temperatures">Creating Temperatures</h2>
|
60
|
+
|
61
|
+
<p>A new temperature can be created in multiple ways:</p>
|
62
|
+
<ul><li>
|
63
|
+
<p>Using keyword arguments:</p>
|
64
|
+
|
65
|
+
<pre><code>Temperature.new(degrees: 0, scale: :celsius)
|
66
|
+
</code></pre>
|
67
|
+
</li><li>
|
68
|
+
<p>Using positional arguments:</p>
|
69
|
+
|
70
|
+
<pre><code>Temperature.new(0, :celsius)
|
71
|
+
</code></pre>
|
72
|
+
</li><li>
|
73
|
+
<p>Even more concise way using <code>Temperature.[]</code> (an alias of <code>Temperature.new</code>):</p>
|
74
|
+
|
75
|
+
<pre><code>Temperature[0, :celsius]
|
76
|
+
</code></pre>
|
77
|
+
</li></ul>
|
78
|
+
|
79
|
+
<h2 id="class-BasicTemperature-label-Creating+Temperatures+from+already+existing+temperature+objects">Creating Temperatures from already existing temperature objects</h2>
|
80
|
+
|
81
|
+
<p>Sometimes it is useful to create a new temperature from already existing one.</p>
|
82
|
+
|
83
|
+
<p>For such cases, there are <a href="BasicTemperature.html#method-i-set_scale">set_degrees}[rdoc-ref:BasicTemperature#set_degrees and {set_scale</a>.</p>
|
84
|
+
|
85
|
+
<p>Since temperatures are <a href="https://martinfowler.com/bliki/ValueObject.html">Value Objects</a>, both methods returns new instances.</p>
|
86
|
+
|
87
|
+
<p>Examples:</p>
|
88
|
+
|
89
|
+
<pre><code>temperature = Temperature[0, :celsius]
|
90
|
+
# => 0 °C
|
91
|
+
|
92
|
+
new_temperature = temperature.set_degrees(15)
|
93
|
+
# => 15 °C
|
94
|
+
|
95
|
+
temperature = Temperature[0, :celsius]
|
96
|
+
# => 0 °C
|
97
|
+
|
98
|
+
new_temperature = temperature.set_scale(:kelvin)
|
99
|
+
# => 0 K
|
100
|
+
</code></pre>
|
101
|
+
|
102
|
+
<h2 id="class-BasicTemperature-label-Conversions">Conversions</h2>
|
103
|
+
|
104
|
+
<p>Temperatures can be converted to diffirent scales.</p>
|
105
|
+
|
106
|
+
<p>Currently, the following scales are supported: <code>Celsius</code>, <code>Fahrenheit</code>, <code>Kelvin</code> and <code>Rankine</code>.</p>
|
107
|
+
|
108
|
+
<pre><code>Temperature[20, :celsius].to_celsius
|
109
|
+
# => 20 °C
|
110
|
+
|
111
|
+
Temperature[20, :celsius].to_fahrenheit
|
112
|
+
# => 68 °F
|
113
|
+
|
114
|
+
Temperature[20, :celsius].to_kelvin
|
115
|
+
# => 293.15 K
|
116
|
+
|
117
|
+
Temperature[20, :celsius].to_rankine
|
118
|
+
# => 527.67 °R
|
119
|
+
</code></pre>
|
120
|
+
|
121
|
+
<p>If it is necessary to convert scale dynamically, <a href="BasicTemperature.html#method-i-to_scale">to_scale</a> method is available.</p>
|
122
|
+
|
123
|
+
<pre><code>Temperature[20, :celsius].to_scale(scale)
|
124
|
+
</code></pre>
|
125
|
+
|
126
|
+
<p>All conversion formulas are taken from <a href="https://www.rapidtables.com/convert/temperature/index.html">RapidTables</a>.</p>
|
127
|
+
|
128
|
+
<p>Conversion precision: 2 accurate digits after the decimal dot.</p>
|
129
|
+
|
130
|
+
<h2 id="class-BasicTemperature-label-Comparison">Comparison</h2>
|
131
|
+
|
132
|
+
<p><a href="BasicTemperature.html"><code>Temperature</code></a> implements idiomatic <a href="https://ruby-doc.org/core/Comparable.html"><=> spaceship operator</a> and mixes in <a href="https://ruby-doc.org/core/Comparable.html">Comparable</a> module.</p>
|
133
|
+
|
134
|
+
<p>As a result, all methods from Comparable are available, e.g:</p>
|
135
|
+
|
136
|
+
<pre><code>Temperature[20, :celsius] < Temperature[25, :celsius]
|
137
|
+
# => true
|
138
|
+
|
139
|
+
Temperature[20, :celsius] <= Temperature[25, :celsius]
|
140
|
+
# => true
|
141
|
+
|
142
|
+
Temperature[20, :celsius] == Temperature[25, :celsius]
|
143
|
+
# => false
|
144
|
+
|
145
|
+
Temperature[20, :celsius] > Temperature[25, :celsius]
|
146
|
+
# => false
|
147
|
+
|
148
|
+
Temperature[20, :celsius] >= Temperature[25, :celsius]
|
149
|
+
# => false
|
150
|
+
|
151
|
+
Temperature[20, :celsius].between?(Temperature[15, :celsius], Temperature[25, :celsius])
|
152
|
+
# => true
|
153
|
+
|
154
|
+
# Starting from Ruby 2.4.6
|
155
|
+
Temperature[20, :celsius].clamp(Temperature[20, :celsius], Temperature[25, :celsius])
|
156
|
+
# => 20 °C
|
157
|
+
</code></pre>
|
158
|
+
|
159
|
+
<p>Please note, if <code>other</code> temperature has a different scale, temperature is automatically converted to that scale before comparison.</p>
|
160
|
+
|
161
|
+
<pre><code>Temperature[20, :celsius] == Temperature[293.15, :kelvin]
|
162
|
+
# => true
|
163
|
+
</code></pre>
|
164
|
+
|
165
|
+
<p>IMPORTANT !!!</p>
|
166
|
+
|
167
|
+
<p><code>degrees</code> are rounded to the nearest value with a precision of 2 decimal digits before comparison.</p>
|
168
|
+
|
169
|
+
<p>This means the following temperatures are considered as equal:</p>
|
170
|
+
|
171
|
+
<pre><code>Temperature[20.020, :celsius] == Temperature[20.024, :celsius]
|
172
|
+
# => true
|
173
|
+
|
174
|
+
Temperature[20.025, :celsius] == Temperature[20.029, :celsius]
|
175
|
+
# => true
|
176
|
+
</code></pre>
|
177
|
+
|
178
|
+
<p>while these ones are treated as NOT equal:</p>
|
179
|
+
|
180
|
+
<pre><code>Temperature[20.024, :celsius] == Temperature[20.029, :celsius]
|
181
|
+
# => false
|
182
|
+
</code></pre>
|
183
|
+
|
184
|
+
<h2 id="class-BasicTemperature-label-Math">Math</h2>
|
185
|
+
|
186
|
+
<h4 id="class-BasicTemperature-label-Addition-2FSubtraction.">Addition/Subtraction.</h4>
|
187
|
+
|
188
|
+
<pre><code>Temperature[20, :celsius] + Temperature[10, :celsius]
|
189
|
+
# => 30 °C
|
190
|
+
|
191
|
+
Temperature[20, :celsius] - Temperature[10, :celsius]
|
192
|
+
# => 10 °C
|
193
|
+
</code></pre>
|
194
|
+
|
195
|
+
<p>If second temperature has a different scale, first temperature is automatically converted to that scale before <code>degrees</code> addition/subtraction.</p>
|
196
|
+
|
197
|
+
<pre><code>Temperature[283.15, :kelvin] + Temperature[10, :celsius]
|
198
|
+
# => 10 °C
|
199
|
+
</code></pre>
|
200
|
+
|
201
|
+
<p>Returned temperature will have the same scale as the second temperature.</p>
|
202
|
+
|
203
|
+
<p>It is possible to add/subtract numerics.</p>
|
204
|
+
|
205
|
+
<pre><code>Temperature[20, :celsius] + 10
|
206
|
+
# => 30 °C
|
207
|
+
|
208
|
+
Temperature[20, :celsius] - 10
|
209
|
+
# => 10 °C
|
210
|
+
</code></pre>
|
211
|
+
|
212
|
+
<p>In such cases, returned temperature will have the same scale as the first temperature.</p>
|
213
|
+
|
214
|
+
<p>Also <a href="https://ruby-doc.org/core/Numeric.html#method-i-coerce">Ruby coersion mechanism</a> is supported.</p>
|
215
|
+
|
216
|
+
<pre><code>10 + Temperature[20, :celsius]
|
217
|
+
# => 30 °C
|
218
|
+
|
219
|
+
10 - Temperature[20, :celsius]
|
220
|
+
# => -10 °C
|
221
|
+
</code></pre>
|
222
|
+
|
223
|
+
<h4 id="class-BasicTemperature-label-Negation">Negation</h4>
|
224
|
+
|
225
|
+
<pre><code>-Temperature[20, :celsius]
|
226
|
+
# => -20 °C
|
227
|
+
</code></pre>
|
228
|
+
|
229
|
+
</div>
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
<!-- Method ref -->
|
245
|
+
<div class="sectiontitle">Methods</div>
|
246
|
+
<dl class="methods">
|
247
|
+
|
248
|
+
<dt>#</dt>
|
249
|
+
<dd>
|
250
|
+
<ul>
|
251
|
+
|
252
|
+
|
253
|
+
<li>
|
254
|
+
<a href="#method-i-2B">+</a>,
|
255
|
+
</li>
|
256
|
+
|
257
|
+
|
258
|
+
<li>
|
259
|
+
<a href="#method-i-2D">-</a>,
|
260
|
+
</li>
|
261
|
+
|
262
|
+
|
263
|
+
<li>
|
264
|
+
<a href="#method-i-2D-40">-@</a>,
|
265
|
+
</li>
|
266
|
+
|
267
|
+
|
268
|
+
<li>
|
269
|
+
<a href="#method-i-3C-3D-3E"><=></a>,
|
270
|
+
</li>
|
271
|
+
|
272
|
+
|
273
|
+
<li>
|
274
|
+
<a href="#method-c-5B-5D">[]</a>
|
275
|
+
</li>
|
276
|
+
|
277
|
+
</ul>
|
278
|
+
</dd>
|
279
|
+
|
280
|
+
<dt>N</dt>
|
281
|
+
<dd>
|
282
|
+
<ul>
|
283
|
+
|
284
|
+
|
285
|
+
<li>
|
286
|
+
<a href="#method-c-new">new</a>
|
287
|
+
</li>
|
288
|
+
|
289
|
+
</ul>
|
290
|
+
</dd>
|
291
|
+
|
292
|
+
<dt>S</dt>
|
293
|
+
<dd>
|
294
|
+
<ul>
|
295
|
+
|
296
|
+
|
297
|
+
<li>
|
298
|
+
<a href="#method-i-set_degrees">set_degrees</a>,
|
299
|
+
</li>
|
300
|
+
|
301
|
+
|
302
|
+
<li>
|
303
|
+
<a href="#method-i-set_scale">set_scale</a>
|
304
|
+
</li>
|
305
|
+
|
306
|
+
</ul>
|
307
|
+
</dd>
|
308
|
+
|
309
|
+
<dt>T</dt>
|
310
|
+
<dd>
|
311
|
+
<ul>
|
312
|
+
|
313
|
+
|
314
|
+
<li>
|
315
|
+
<a href="#method-i-to_celsius">to_celsius</a>,
|
316
|
+
</li>
|
317
|
+
|
318
|
+
|
319
|
+
<li>
|
320
|
+
<a href="#method-i-to_fahrenheit">to_fahrenheit</a>,
|
321
|
+
</li>
|
322
|
+
|
323
|
+
|
324
|
+
<li>
|
325
|
+
<a href="#method-i-to_kelvin">to_kelvin</a>,
|
326
|
+
</li>
|
327
|
+
|
328
|
+
|
329
|
+
<li>
|
330
|
+
<a href="#method-i-to_rankine">to_rankine</a>,
|
331
|
+
</li>
|
332
|
+
|
333
|
+
|
334
|
+
<li>
|
335
|
+
<a href="#method-i-to_scale">to_scale</a>
|
336
|
+
</li>
|
337
|
+
|
338
|
+
</ul>
|
339
|
+
</dd>
|
340
|
+
|
341
|
+
</dl>
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
<!-- Includes -->
|
346
|
+
<div class="sectiontitle">Included Modules</div>
|
347
|
+
<ul>
|
348
|
+
|
349
|
+
<li>
|
350
|
+
|
351
|
+
Comparable
|
352
|
+
|
353
|
+
</li>
|
354
|
+
|
355
|
+
</ul>
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
<!-- Section constants -->
|
368
|
+
<div class="sectiontitle">Constants</div>
|
369
|
+
<table border='0' cellpadding='5'>
|
370
|
+
|
371
|
+
<tr valign='top'>
|
372
|
+
<td class="attr-name">CELSIUS</td>
|
373
|
+
<td>=</td>
|
374
|
+
<td class="attr-value">'celsius'</td>
|
375
|
+
</tr>
|
376
|
+
|
377
|
+
<tr valign='top'>
|
378
|
+
<td> </td>
|
379
|
+
<td colspan="2" class="attr-desc"></td>
|
380
|
+
</tr>
|
381
|
+
|
382
|
+
|
383
|
+
<tr valign='top'>
|
384
|
+
<td class="attr-name">FAHRENHEIT</td>
|
385
|
+
<td>=</td>
|
386
|
+
<td class="attr-value">'fahrenheit'</td>
|
387
|
+
</tr>
|
388
|
+
|
389
|
+
<tr valign='top'>
|
390
|
+
<td> </td>
|
391
|
+
<td colspan="2" class="attr-desc"></td>
|
392
|
+
</tr>
|
393
|
+
|
394
|
+
|
395
|
+
<tr valign='top'>
|
396
|
+
<td class="attr-name">KELVIN</td>
|
397
|
+
<td>=</td>
|
398
|
+
<td class="attr-value">'kelvin'</td>
|
399
|
+
</tr>
|
400
|
+
|
401
|
+
<tr valign='top'>
|
402
|
+
<td> </td>
|
403
|
+
<td colspan="2" class="attr-desc"></td>
|
404
|
+
</tr>
|
405
|
+
|
406
|
+
|
407
|
+
<tr valign='top'>
|
408
|
+
<td class="attr-name">RANKINE</td>
|
409
|
+
<td>=</td>
|
410
|
+
<td class="attr-value">'rankine'</td>
|
411
|
+
</tr>
|
412
|
+
|
413
|
+
<tr valign='top'>
|
414
|
+
<td> </td>
|
415
|
+
<td colspan="2" class="attr-desc"></td>
|
416
|
+
</tr>
|
417
|
+
|
418
|
+
|
419
|
+
<tr valign='top'>
|
420
|
+
<td class="attr-name">SCALES</td>
|
421
|
+
<td>=</td>
|
422
|
+
<td class="attr-value">[CELSIUS, FAHRENHEIT, KELVIN, RANKINE].freeze</td>
|
423
|
+
</tr>
|
424
|
+
|
425
|
+
<tr valign='top'>
|
426
|
+
<td> </td>
|
427
|
+
<td colspan="2" class="attr-desc"><p>A list of all currently supported scale values.</p></td>
|
428
|
+
</tr>
|
429
|
+
|
430
|
+
|
431
|
+
<tr valign='top'>
|
432
|
+
<td class="attr-name">VERSION</td>
|
433
|
+
<td>=</td>
|
434
|
+
<td class="attr-value">'0.2.1'</td>
|
435
|
+
</tr>
|
436
|
+
|
437
|
+
<tr valign='top'>
|
438
|
+
<td> </td>
|
439
|
+
<td colspan="2" class="attr-desc"></td>
|
440
|
+
</tr>
|
441
|
+
|
442
|
+
|
443
|
+
</table>
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
|
448
|
+
<!-- Section attributes -->
|
449
|
+
<div class="sectiontitle">Attributes</div>
|
450
|
+
<table border='0' cellpadding='5'>
|
451
|
+
|
452
|
+
<tr valign='top'>
|
453
|
+
<td class='attr-rw'>
|
454
|
+
[R]
|
455
|
+
</td>
|
456
|
+
<td class='attr-name'>degrees</td>
|
457
|
+
<td class='attr-desc'><p>Degrees of the temperature.</p></td>
|
458
|
+
</tr>
|
459
|
+
|
460
|
+
<tr valign='top'>
|
461
|
+
<td class='attr-rw'>
|
462
|
+
[R]
|
463
|
+
</td>
|
464
|
+
<td class='attr-name'>scale</td>
|
465
|
+
<td class='attr-desc'><p>Scale of the temperature. Look at <a href="BasicTemperature.html#SCALES">SCALES</a> for possible values.</p></td>
|
466
|
+
</tr>
|
467
|
+
|
468
|
+
</table>
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
<!-- Methods -->
|
473
|
+
|
474
|
+
<div class="sectiontitle">Class Public methods</div>
|
475
|
+
|
476
|
+
<div class="method">
|
477
|
+
<div class="title method-title" id="method-c-5B-5D">
|
478
|
+
|
479
|
+
<b>[](degrees:, scale:)<br />[](degrees, scale)</b>
|
480
|
+
|
481
|
+
<a href="../classes/BasicTemperature.html#method-c-5B-5D" name="method-c-5B-5D" class="permalink">Link</a>
|
482
|
+
</div>
|
483
|
+
|
484
|
+
|
485
|
+
<div class="description">
|
486
|
+
<p>Creates a new instance of <a href="Temperature.html"><code>Temperature</code></a>. Alias for <code>new</code>.</p>
|
487
|
+
</div>
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
|
496
|
+
<div class="sourcecode">
|
497
|
+
|
498
|
+
<p class="source-link">
|
499
|
+
Source:
|
500
|
+
<a href="javascript:toggleSource('method-c-5B-5D_source')" id="l_method-c-5B-5D_source">show</a>
|
501
|
+
|
502
|
+
</p>
|
503
|
+
<div id="method-c-5B-5D_source" class="dyn-source">
|
504
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 215</span>
|
505
|
+
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">[]</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kwargs</span>)
|
506
|
+
<span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kwargs</span>)
|
507
|
+
<span class="ruby-keyword">end</span></pre>
|
508
|
+
</div>
|
509
|
+
</div>
|
510
|
+
|
511
|
+
</div>
|
512
|
+
|
513
|
+
<div class="method">
|
514
|
+
<div class="title method-title" id="method-c-new">
|
515
|
+
|
516
|
+
<b>new(degrees:, scale:)<br />new(degrees, scale)</b>
|
517
|
+
|
518
|
+
<a href="../classes/BasicTemperature.html#method-c-new" name="method-c-new" class="permalink">Link</a>
|
519
|
+
</div>
|
520
|
+
|
521
|
+
|
522
|
+
<div class="description">
|
523
|
+
<p>Creates a new instance of <a href="Temperature.html"><code>Temperature</code></a>. Is aliased as <code>[]</code>.</p>
|
524
|
+
</div>
|
525
|
+
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
<div class="sourcecode">
|
534
|
+
|
535
|
+
<p class="source-link">
|
536
|
+
Source:
|
537
|
+
<a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
|
538
|
+
|
539
|
+
</p>
|
540
|
+
<div id="method-c-new_source" class="dyn-source">
|
541
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 226</span>
|
542
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">positional_arguments</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">keyword_arguments</span>)
|
543
|
+
<span class="ruby-identifier">assert_either_positional_arguments_or_keyword_arguments!</span>(<span class="ruby-identifier">positional_arguments</span>, <span class="ruby-identifier">keyword_arguments</span>)
|
544
|
+
|
545
|
+
<span class="ruby-keyword">if</span> <span class="ruby-identifier">keyword_arguments</span>.<span class="ruby-identifier">any?</span>
|
546
|
+
<span class="ruby-identifier">initialize_via_keywords_arguments</span>(<span class="ruby-identifier">keyword_arguments</span>)
|
547
|
+
<span class="ruby-keyword">else</span> <span class="ruby-comment"># positional_arguments.any?</span>
|
548
|
+
<span class="ruby-identifier">initialize_via_positional_arguments</span>(<span class="ruby-identifier">positional_arguments</span>)
|
549
|
+
<span class="ruby-keyword">end</span>
|
550
|
+
<span class="ruby-keyword">end</span></pre>
|
551
|
+
</div>
|
552
|
+
</div>
|
553
|
+
|
554
|
+
</div>
|
555
|
+
|
556
|
+
|
557
|
+
|
558
|
+
<div class="sectiontitle">Instance Public methods</div>
|
559
|
+
|
560
|
+
<div class="method">
|
561
|
+
<div class="title method-title" id="method-i-2B">
|
562
|
+
|
563
|
+
<b>+(temperature)<br />+(numeric)</b>
|
564
|
+
|
565
|
+
<a href="../classes/BasicTemperature.html#method-i-2B" name="method-i-2B" class="permalink">Link</a>
|
566
|
+
</div>
|
567
|
+
|
568
|
+
|
569
|
+
<div class="description">
|
570
|
+
<p>Performs addition. Returns a new <a href="Temperature.html"><code>Temperature</code></a>.</p>
|
571
|
+
|
572
|
+
<pre><code>Temperature[20, :celsius] + Temperature[10, :celsius]
|
573
|
+
# => 30 °C
|
574
|
+
</code></pre>
|
575
|
+
|
576
|
+
<p>If the second temperature has a different scale, the first temperature is automatically converted to that scale before <code>degrees</code> addition.</p>
|
577
|
+
|
578
|
+
<pre><code>Temperature[283.15, :kelvin] + Temperature[20, :celsius]
|
579
|
+
# => 30 °C
|
580
|
+
</code></pre>
|
581
|
+
|
582
|
+
<p>Returned temperature will have the same scale as the second temperature.</p>
|
583
|
+
|
584
|
+
<p>It is possible to add numerics.</p>
|
585
|
+
|
586
|
+
<pre><code>Temperature[20, :celsius] + 10
|
587
|
+
# => 30 °C
|
588
|
+
</code></pre>
|
589
|
+
|
590
|
+
<p>In such cases, returned temperature will have the same scale as the first temperature.</p>
|
591
|
+
|
592
|
+
<p>Also <a href="https://ruby-doc.org/core/Numeric.html#method-i-coerce">Ruby coersion mechanism</a> is supported.</p>
|
593
|
+
|
594
|
+
<pre><code>10 + Temperature[20, :celsius]
|
595
|
+
# => 30 °C
|
596
|
+
</code></pre>
|
597
|
+
</div>
|
598
|
+
|
599
|
+
|
600
|
+
|
601
|
+
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
|
606
|
+
<div class="sourcecode">
|
607
|
+
|
608
|
+
<p class="source-link">
|
609
|
+
Source:
|
610
|
+
<a href="javascript:toggleSource('method-i-2B_source')" id="l_method-i-2B_source">show</a>
|
611
|
+
|
612
|
+
</p>
|
613
|
+
<div id="method-i-2B_source" class="dyn-source">
|
614
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 493</span>
|
615
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">+</span>(<span class="ruby-identifier">other</span>)
|
616
|
+
<span class="ruby-identifier">assert_numeric_or_temperature!</span>(<span class="ruby-identifier">other</span>)
|
617
|
+
|
618
|
+
<span class="ruby-identifier">degrees</span>, <span class="ruby-identifier">scale</span> =
|
619
|
+
<span class="ruby-keyword">case</span> <span class="ruby-identifier">other</span>
|
620
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">Numeric</span>
|
621
|
+
[<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">other</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span>]
|
622
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">BasicTemperature</span>
|
623
|
+
[<span class="ruby-keyword">self</span>.<span class="ruby-identifier">to_scale</span>(<span class="ruby-identifier">other</span>.<span class="ruby-identifier">scale</span>).<span class="ruby-identifier">degrees</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">other</span>.<span class="ruby-identifier">degrees</span>, <span class="ruby-identifier">other</span>.<span class="ruby-identifier">scale</span>]
|
624
|
+
<span class="ruby-keyword">end</span>
|
625
|
+
|
626
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-identifier">scale</span>)
|
627
|
+
<span class="ruby-keyword">end</span></pre>
|
628
|
+
</div>
|
629
|
+
</div>
|
630
|
+
|
631
|
+
</div>
|
632
|
+
|
633
|
+
<div class="method">
|
634
|
+
<div class="title method-title" id="method-i-2D">
|
635
|
+
|
636
|
+
<b>-(temperature)<br />-(numeric)</b>
|
637
|
+
|
638
|
+
<a href="../classes/BasicTemperature.html#method-i-2D" name="method-i-2D" class="permalink">Link</a>
|
639
|
+
</div>
|
640
|
+
|
641
|
+
|
642
|
+
<div class="description">
|
643
|
+
<p>Performs subtraction. Returns a new <a href="Temperature.html"><code>Temperature</code></a>.</p>
|
644
|
+
|
645
|
+
<pre><code>Temperature[20, :celsius] - Temperature[10, :celsius]
|
646
|
+
# => 10 °C
|
647
|
+
</code></pre>
|
648
|
+
|
649
|
+
<p>If the second temperature has a different scale, the first temperature is automatically converted to that scale before <code>degrees</code> subtraction.</p>
|
650
|
+
|
651
|
+
<pre><code>Temperature[283.15, :kelvin] + Temperature[10, :celsius]
|
652
|
+
# => 10 °C
|
653
|
+
</code></pre>
|
654
|
+
|
655
|
+
<p>Returned temperature will have the same scale as the second temperature.</p>
|
656
|
+
|
657
|
+
<p>It is possible to subtract numerics.</p>
|
658
|
+
|
659
|
+
<pre><code>Temperature[20, :celsius] - 10
|
660
|
+
# => 10 °C
|
661
|
+
</code></pre>
|
662
|
+
|
663
|
+
<p>In such cases, returned temperature will have the same scale as the first temperature.</p>
|
664
|
+
|
665
|
+
<p>Also <a href="https://ruby-doc.org/core/Numeric.html#method-i-coerce">Ruby coersion mechanism</a> is supported.</p>
|
666
|
+
|
667
|
+
<pre><code>10 - Temperature[20, :celsius]
|
668
|
+
# => -10 °C
|
669
|
+
</code></pre>
|
670
|
+
</div>
|
671
|
+
|
672
|
+
|
673
|
+
|
674
|
+
|
675
|
+
|
676
|
+
|
677
|
+
|
678
|
+
|
679
|
+
<div class="sourcecode">
|
680
|
+
|
681
|
+
<p class="source-link">
|
682
|
+
Source:
|
683
|
+
<a href="javascript:toggleSource('method-i-2D_source')" id="l_method-i-2D_source">show</a>
|
684
|
+
|
685
|
+
</p>
|
686
|
+
<div id="method-i-2D_source" class="dyn-source">
|
687
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 537</span>
|
688
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">-</span>(<span class="ruby-identifier">other</span>)
|
689
|
+
<span class="ruby-keyword">self</span> <span class="ruby-operator">+</span> <span class="ruby-operator">-</span><span class="ruby-identifier">other</span>
|
690
|
+
<span class="ruby-keyword">end</span></pre>
|
691
|
+
</div>
|
692
|
+
</div>
|
693
|
+
|
694
|
+
</div>
|
695
|
+
|
696
|
+
<div class="method">
|
697
|
+
<div class="title method-title" id="method-i-2D-40">
|
698
|
+
|
699
|
+
<b>-@</b>()
|
700
|
+
|
701
|
+
<a href="../classes/BasicTemperature.html#method-i-2D-40" name="method-i-2D-40" class="permalink">Link</a>
|
702
|
+
</div>
|
703
|
+
|
704
|
+
|
705
|
+
<div class="description">
|
706
|
+
<p>Returns a new <a href="Temperature.html"><code>Temperature</code></a> with negated <code>degrees</code>.</p>
|
707
|
+
|
708
|
+
<pre><code>-Temperature[20, :celsius]
|
709
|
+
# => -20 °C
|
710
|
+
</code></pre>
|
711
|
+
</div>
|
712
|
+
|
713
|
+
|
714
|
+
|
715
|
+
|
716
|
+
|
717
|
+
|
718
|
+
|
719
|
+
|
720
|
+
<div class="sourcecode">
|
721
|
+
|
722
|
+
<p class="source-link">
|
723
|
+
Source:
|
724
|
+
<a href="javascript:toggleSource('method-i-2D-40_source')" id="l_method-i-2D-40_source">show</a>
|
725
|
+
|
726
|
+
</p>
|
727
|
+
<div id="method-i-2D-40_source" class="dyn-source">
|
728
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 547</span>
|
729
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">-@</span>
|
730
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">-</span><span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span>)
|
731
|
+
<span class="ruby-keyword">end</span></pre>
|
732
|
+
</div>
|
733
|
+
</div>
|
734
|
+
|
735
|
+
</div>
|
736
|
+
|
737
|
+
<div class="method">
|
738
|
+
<div class="title method-title" id="method-i-3C-3D-3E">
|
739
|
+
|
740
|
+
<b><=></b>(other)
|
741
|
+
|
742
|
+
<a href="../classes/BasicTemperature.html#method-i-3C-3D-3E" name="method-i-3C-3D-3E" class="permalink">Link</a>
|
743
|
+
</div>
|
744
|
+
|
745
|
+
|
746
|
+
<div class="description">
|
747
|
+
<p>Compares temperture with <code>other</code> temperature.</p>
|
748
|
+
|
749
|
+
<p>Returns <code>0</code> if they are considered as equal.</p>
|
750
|
+
|
751
|
+
<p>Two temperatures are considered as equal when they have the same amount of <code>degrees</code>.</p>
|
752
|
+
|
753
|
+
<p>Returns <code>-1</code> if temperature is lower than <code>other</code> temperature.</p>
|
754
|
+
|
755
|
+
<p>Returns <code>1</code> if temperature is higher than <code>other</code> temperature.</p>
|
756
|
+
|
757
|
+
<p>If <code>other</code> temperature has a different scale, temperature is automatically converted to that scale before <code>degrees</code> comparison.</p>
|
758
|
+
|
759
|
+
<pre><code>Temperature[20, :celsius] <=> Temperature[20, :celsius]
|
760
|
+
# => 0
|
761
|
+
|
762
|
+
Temperature[20, :celsius] <=> Temperature[293.15, :kelvin]
|
763
|
+
# => 0
|
764
|
+
</code></pre>
|
765
|
+
|
766
|
+
<p>IMPORTANT!!!</p>
|
767
|
+
|
768
|
+
<p>This method rounds <code>degrees</code> to the nearest value with a precision of 2 decimal digits.</p>
|
769
|
+
|
770
|
+
<p>This means the following:</p>
|
771
|
+
|
772
|
+
<pre><code>Temperature[20.020, :celsius] <=> Temperature[20.024, :celsius]
|
773
|
+
# => 0
|
774
|
+
|
775
|
+
Temperature[20.025, :celsius] <=> Temperature[20.029, :celsius]
|
776
|
+
# => 0
|
777
|
+
|
778
|
+
Temperature[20.024, :celsius] <=> Temperature[20.029, :celsius]
|
779
|
+
# => -1
|
780
|
+
</code></pre>
|
781
|
+
</div>
|
782
|
+
|
783
|
+
|
784
|
+
|
785
|
+
|
786
|
+
|
787
|
+
|
788
|
+
|
789
|
+
|
790
|
+
<div class="sourcecode">
|
791
|
+
|
792
|
+
<p class="source-link">
|
793
|
+
Source:
|
794
|
+
<a href="javascript:toggleSource('method-i-3C-3D-3E_source')" id="l_method-i-3C-3D-3E_source">show</a>
|
795
|
+
|
796
|
+
</p>
|
797
|
+
<div id="method-i-3C-3D-3E_source" class="dyn-source">
|
798
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 457</span>
|
799
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title"><=></span>(<span class="ruby-identifier">other</span>)
|
800
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">assert_temperature</span>(<span class="ruby-identifier">other</span>)
|
801
|
+
|
802
|
+
<span class="ruby-identifier">compare_degrees</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">to_scale</span>(<span class="ruby-identifier">other</span>.<span class="ruby-identifier">scale</span>).<span class="ruby-identifier">degrees</span>, <span class="ruby-identifier">other</span>.<span class="ruby-identifier">degrees</span>)
|
803
|
+
<span class="ruby-keyword">end</span></pre>
|
804
|
+
</div>
|
805
|
+
</div>
|
806
|
+
|
807
|
+
</div>
|
808
|
+
|
809
|
+
<div class="method">
|
810
|
+
<div class="title method-title" id="method-i-set_degrees">
|
811
|
+
|
812
|
+
<b>set_degrees</b>(degrees)
|
813
|
+
|
814
|
+
<a href="../classes/BasicTemperature.html#method-i-set_degrees" name="method-i-set_degrees" class="permalink">Link</a>
|
815
|
+
</div>
|
816
|
+
|
817
|
+
|
818
|
+
<div class="description">
|
819
|
+
<p>Returns a new <a href="Temperature.html"><code>Temperature</code></a> with updated <code>degrees</code>.</p>
|
820
|
+
|
821
|
+
<pre><code>temperature = Temperature[0, :celsius]
|
822
|
+
# => 0 °C
|
823
|
+
|
824
|
+
new_temperature = temperature.set_degrees(15)
|
825
|
+
# => 15 °C
|
826
|
+
</code></pre>
|
827
|
+
</div>
|
828
|
+
|
829
|
+
|
830
|
+
|
831
|
+
|
832
|
+
|
833
|
+
|
834
|
+
|
835
|
+
|
836
|
+
<div class="sourcecode">
|
837
|
+
|
838
|
+
<p class="source-link">
|
839
|
+
Source:
|
840
|
+
<a href="javascript:toggleSource('method-i-set_degrees_source')" id="l_method-i-set_degrees_source">show</a>
|
841
|
+
|
842
|
+
</p>
|
843
|
+
<div id="method-i-set_degrees_source" class="dyn-source">
|
844
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 246</span>
|
845
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">set_degrees</span>(<span class="ruby-identifier">degrees</span>)
|
846
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-identifier">scale</span>)
|
847
|
+
<span class="ruby-keyword">end</span></pre>
|
848
|
+
</div>
|
849
|
+
</div>
|
850
|
+
|
851
|
+
</div>
|
852
|
+
|
853
|
+
<div class="method">
|
854
|
+
<div class="title method-title" id="method-i-set_scale">
|
855
|
+
|
856
|
+
<b>set_scale</b>(scale)
|
857
|
+
|
858
|
+
<a href="../classes/BasicTemperature.html#method-i-set_scale" name="method-i-set_scale" class="permalink">Link</a>
|
859
|
+
</div>
|
860
|
+
|
861
|
+
|
862
|
+
<div class="description">
|
863
|
+
<p>Returns a new <a href="Temperature.html"><code>Temperature</code></a> with updated <code>scale</code>.</p>
|
864
|
+
|
865
|
+
<pre><code>temperature = Temperature[0, :celsius]
|
866
|
+
# => 0 °C
|
867
|
+
|
868
|
+
new_temperature = temperature.set_scale(:kelvin)
|
869
|
+
# => 0 K
|
870
|
+
</code></pre>
|
871
|
+
</div>
|
872
|
+
|
873
|
+
|
874
|
+
|
875
|
+
|
876
|
+
|
877
|
+
|
878
|
+
|
879
|
+
|
880
|
+
<div class="sourcecode">
|
881
|
+
|
882
|
+
<p class="source-link">
|
883
|
+
Source:
|
884
|
+
<a href="javascript:toggleSource('method-i-set_scale_source')" id="l_method-i-set_scale_source">show</a>
|
885
|
+
|
886
|
+
</p>
|
887
|
+
<div id="method-i-set_scale_source" class="dyn-source">
|
888
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 261</span>
|
889
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">set_scale</span>(<span class="ruby-identifier">scale</span>)
|
890
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-identifier">scale</span>)
|
891
|
+
<span class="ruby-keyword">end</span></pre>
|
892
|
+
</div>
|
893
|
+
</div>
|
894
|
+
|
895
|
+
</div>
|
896
|
+
|
897
|
+
<div class="method">
|
898
|
+
<div class="title method-title" id="method-i-to_celsius">
|
899
|
+
|
900
|
+
<b>to_celsius</b>()
|
901
|
+
|
902
|
+
<a href="../classes/BasicTemperature.html#method-i-to_celsius" name="method-i-to_celsius" class="permalink">Link</a>
|
903
|
+
</div>
|
904
|
+
|
905
|
+
|
906
|
+
<div class="description">
|
907
|
+
<p>Converts temperature to Celsius scale. If temperature is already in Celsius, returns current temperature object.</p>
|
908
|
+
|
909
|
+
<p>Memoizes subsequent calls.</p>
|
910
|
+
|
911
|
+
<p>Conversion formulas are taken from <a href="https://www.rapidtables.com/">RapidTables</a>:</p>
|
912
|
+
<ol><li>
|
913
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/celsius-to-fahrenheit.html">Celsius to Fahrenheit</a>.</p>
|
914
|
+
</li><li>
|
915
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/celsius-to-kelvin.html">Celsius to Kelvin</a>.</p>
|
916
|
+
</li><li>
|
917
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/celsius-to-rankine.html">Celsius to Rankine</a>.</p>
|
918
|
+
</li></ol>
|
919
|
+
|
920
|
+
<pre><code>Temperature[0, :fahrenheit].to_celsius
|
921
|
+
# => -17.78 °C
|
922
|
+
</code></pre>
|
923
|
+
</div>
|
924
|
+
|
925
|
+
|
926
|
+
|
927
|
+
|
928
|
+
|
929
|
+
|
930
|
+
|
931
|
+
|
932
|
+
<div class="sourcecode">
|
933
|
+
|
934
|
+
<p class="source-link">
|
935
|
+
Source:
|
936
|
+
<a href="javascript:toggleSource('method-i-to_celsius_source')" id="l_method-i-to_celsius_source">show</a>
|
937
|
+
|
938
|
+
</p>
|
939
|
+
<div id="method-i-to_celsius_source" class="dyn-source">
|
940
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 308</span>
|
941
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_celsius</span>
|
942
|
+
<span class="ruby-identifier">memoized</span>(<span class="ruby-value">:to_celsius</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">memoize</span>(<span class="ruby-value">:to_celsius</span>, <span class="ruby-operator">-></span> {
|
943
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span> <span class="ruby-operator">==</span> <span class="ruby-constant">CELSIUS</span>
|
944
|
+
|
945
|
+
<span class="ruby-identifier">degrees</span> =
|
946
|
+
<span class="ruby-keyword">case</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span>
|
947
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">FAHRENHEIT</span>
|
948
|
+
(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">-</span> <span class="ruby-value">32</span>) <span class="ruby-operator">*</span> (<span class="ruby-value">5</span> <span class="ruby-operator">/</span> <span class="ruby-value">9r</span>)
|
949
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">KELVIN</span>
|
950
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">-</span> <span class="ruby-value">273.15</span>
|
951
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">RANKINE</span>
|
952
|
+
(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">-</span> <span class="ruby-value">491.67</span>) <span class="ruby-operator">*</span> (<span class="ruby-value">5</span> <span class="ruby-operator">/</span> <span class="ruby-value">9r</span>)
|
953
|
+
<span class="ruby-keyword">end</span>
|
954
|
+
|
955
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-constant">CELSIUS</span>)
|
956
|
+
})
|
957
|
+
<span class="ruby-keyword">end</span></pre>
|
958
|
+
</div>
|
959
|
+
</div>
|
960
|
+
|
961
|
+
</div>
|
962
|
+
|
963
|
+
<div class="method">
|
964
|
+
<div class="title method-title" id="method-i-to_fahrenheit">
|
965
|
+
|
966
|
+
<b>to_fahrenheit</b>()
|
967
|
+
|
968
|
+
<a href="../classes/BasicTemperature.html#method-i-to_fahrenheit" name="method-i-to_fahrenheit" class="permalink">Link</a>
|
969
|
+
</div>
|
970
|
+
|
971
|
+
|
972
|
+
<div class="description">
|
973
|
+
<p>Converts temperature to Fahrenheit scale. If temperature is already in Fahrenheit, returns current temperature object.</p>
|
974
|
+
|
975
|
+
<p>Memoizes subsequent calls.</p>
|
976
|
+
|
977
|
+
<p>Conversion formulas are taken from <a href="https://www.rapidtables.com/">RapidTables</a>:</p>
|
978
|
+
<ol><li>
|
979
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/fahrenheit-to-celsius.html">Fahrenheit to Celsius</a>.</p>
|
980
|
+
</li><li>
|
981
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.html">Fahrenheit to Kelvin</a>.</p>
|
982
|
+
</li><li>
|
983
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/fahrenheit-to-rankine.html">Fahrenheit to Rankine</a>.</p>
|
984
|
+
</li></ol>
|
985
|
+
|
986
|
+
<pre><code>Temperature[0, :celsius].to_fahrenheit
|
987
|
+
# => 32 °F
|
988
|
+
</code></pre>
|
989
|
+
</div>
|
990
|
+
|
991
|
+
|
992
|
+
|
993
|
+
|
994
|
+
|
995
|
+
|
996
|
+
|
997
|
+
|
998
|
+
<div class="sourcecode">
|
999
|
+
|
1000
|
+
<p class="source-link">
|
1001
|
+
Source:
|
1002
|
+
<a href="javascript:toggleSource('method-i-to_fahrenheit_source')" id="l_method-i-to_fahrenheit_source">show</a>
|
1003
|
+
|
1004
|
+
</p>
|
1005
|
+
<div id="method-i-to_fahrenheit_source" class="dyn-source">
|
1006
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 340</span>
|
1007
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_fahrenheit</span>
|
1008
|
+
<span class="ruby-identifier">memoized</span>(<span class="ruby-value">:to_fahrenheit</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">memoize</span>(<span class="ruby-value">:to_fahrenheit</span>, <span class="ruby-operator">-></span> {
|
1009
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span> <span class="ruby-operator">==</span> <span class="ruby-constant">FAHRENHEIT</span>
|
1010
|
+
|
1011
|
+
<span class="ruby-identifier">degrees</span> =
|
1012
|
+
<span class="ruby-keyword">case</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span>
|
1013
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">CELSIUS</span>
|
1014
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">*</span> (<span class="ruby-value">9</span> <span class="ruby-operator">/</span> <span class="ruby-value">5r</span>) <span class="ruby-operator">+</span> <span class="ruby-value">32</span>
|
1015
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">KELVIN</span>
|
1016
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">*</span> (<span class="ruby-value">9</span> <span class="ruby-operator">/</span> <span class="ruby-value">5r</span>) <span class="ruby-operator">-</span> <span class="ruby-value">459.67</span>
|
1017
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">RANKINE</span>
|
1018
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">-</span> <span class="ruby-value">459.67</span>
|
1019
|
+
<span class="ruby-keyword">end</span>
|
1020
|
+
|
1021
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-constant">FAHRENHEIT</span>)
|
1022
|
+
})
|
1023
|
+
<span class="ruby-keyword">end</span></pre>
|
1024
|
+
</div>
|
1025
|
+
</div>
|
1026
|
+
|
1027
|
+
</div>
|
1028
|
+
|
1029
|
+
<div class="method">
|
1030
|
+
<div class="title method-title" id="method-i-to_kelvin">
|
1031
|
+
|
1032
|
+
<b>to_kelvin</b>()
|
1033
|
+
|
1034
|
+
<a href="../classes/BasicTemperature.html#method-i-to_kelvin" name="method-i-to_kelvin" class="permalink">Link</a>
|
1035
|
+
</div>
|
1036
|
+
|
1037
|
+
|
1038
|
+
<div class="description">
|
1039
|
+
<p>Converts temperature to Kelvin scale. If temperature is already in Kelvin, returns current</p>
|
1040
|
+
|
1041
|
+
<pre><code>temperature object.
|
1042
|
+
</code></pre>
|
1043
|
+
|
1044
|
+
<p>Memoizes subsequent calls.</p>
|
1045
|
+
|
1046
|
+
<p>Conversion formulas are taken from <a href="https://www.rapidtables.com/">RapidTables</a>:</p>
|
1047
|
+
<ol><li>
|
1048
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/kelvin-to-celsius.html">Kelvin to Celsius</a>.</p>
|
1049
|
+
</li><li>
|
1050
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/kelvin-to-fahrenheit.html">Kelvin to Fahrenheit</a>.</p>
|
1051
|
+
</li><li>
|
1052
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/kelvin-to-rankine.html">Kelvin to Rankine</a>.</p>
|
1053
|
+
</li></ol>
|
1054
|
+
|
1055
|
+
<pre><code>Temperature[0, :kelvin].to_rankine
|
1056
|
+
# => 0 °R
|
1057
|
+
</code></pre>
|
1058
|
+
</div>
|
1059
|
+
|
1060
|
+
|
1061
|
+
|
1062
|
+
|
1063
|
+
|
1064
|
+
|
1065
|
+
|
1066
|
+
|
1067
|
+
<div class="sourcecode">
|
1068
|
+
|
1069
|
+
<p class="source-link">
|
1070
|
+
Source:
|
1071
|
+
<a href="javascript:toggleSource('method-i-to_kelvin_source')" id="l_method-i-to_kelvin_source">show</a>
|
1072
|
+
|
1073
|
+
</p>
|
1074
|
+
<div id="method-i-to_kelvin_source" class="dyn-source">
|
1075
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 372</span>
|
1076
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_kelvin</span>
|
1077
|
+
<span class="ruby-identifier">memoized</span>(<span class="ruby-value">:to_kelvin</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">memoize</span>(<span class="ruby-value">:to_kelvin</span>, <span class="ruby-operator">-></span> {
|
1078
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span> <span class="ruby-operator">==</span> <span class="ruby-constant">KELVIN</span>
|
1079
|
+
|
1080
|
+
<span class="ruby-identifier">degrees</span> =
|
1081
|
+
<span class="ruby-keyword">case</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span>
|
1082
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">CELSIUS</span>
|
1083
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">+</span> <span class="ruby-value">273.15</span>
|
1084
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">FAHRENHEIT</span>
|
1085
|
+
(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">+</span> <span class="ruby-value">459.67</span>) <span class="ruby-operator">*</span> (<span class="ruby-value">5</span> <span class="ruby-operator">/</span> <span class="ruby-value">9r</span>)
|
1086
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">RANKINE</span>
|
1087
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">*</span> (<span class="ruby-value">5</span> <span class="ruby-operator">/</span> <span class="ruby-value">9r</span>)
|
1088
|
+
<span class="ruby-keyword">end</span>
|
1089
|
+
|
1090
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-constant">KELVIN</span>)
|
1091
|
+
})
|
1092
|
+
<span class="ruby-keyword">end</span></pre>
|
1093
|
+
</div>
|
1094
|
+
</div>
|
1095
|
+
|
1096
|
+
</div>
|
1097
|
+
|
1098
|
+
<div class="method">
|
1099
|
+
<div class="title method-title" id="method-i-to_rankine">
|
1100
|
+
|
1101
|
+
<b>to_rankine</b>()
|
1102
|
+
|
1103
|
+
<a href="../classes/BasicTemperature.html#method-i-to_rankine" name="method-i-to_rankine" class="permalink">Link</a>
|
1104
|
+
</div>
|
1105
|
+
|
1106
|
+
|
1107
|
+
<div class="description">
|
1108
|
+
<p>Converts temperature to Rankine scale. If temperature is already in Rankine, returns current temperature object.</p>
|
1109
|
+
|
1110
|
+
<p>Memoizes subsequent calls.</p>
|
1111
|
+
|
1112
|
+
<p>Conversion formulas are taken from <a href="https://www.rapidtables.com/">RapidTables</a>:</p>
|
1113
|
+
<ol><li>
|
1114
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/rankine-to-celsius.html">Rankine to Celsius</a>.</p>
|
1115
|
+
</li><li>
|
1116
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/rankine-to-fahrenheit.html">Rankine to Fahrenheit</a>.</p>
|
1117
|
+
</li><li>
|
1118
|
+
<p><a href="https://www.rapidtables.com/convert/temperature/rankine-to-kelvin.html">Rankine to Kelvin</a>.</p>
|
1119
|
+
</li></ol>
|
1120
|
+
|
1121
|
+
<pre><code>Temperature[0, :rankine].to_kelvin
|
1122
|
+
# => 0 K
|
1123
|
+
</code></pre>
|
1124
|
+
</div>
|
1125
|
+
|
1126
|
+
|
1127
|
+
|
1128
|
+
|
1129
|
+
|
1130
|
+
|
1131
|
+
|
1132
|
+
|
1133
|
+
<div class="sourcecode">
|
1134
|
+
|
1135
|
+
<p class="source-link">
|
1136
|
+
Source:
|
1137
|
+
<a href="javascript:toggleSource('method-i-to_rankine_source')" id="l_method-i-to_rankine_source">show</a>
|
1138
|
+
|
1139
|
+
</p>
|
1140
|
+
<div id="method-i-to_rankine_source" class="dyn-source">
|
1141
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 404</span>
|
1142
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_rankine</span>
|
1143
|
+
<span class="ruby-identifier">memoized</span>(<span class="ruby-value">:to_rankine</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">memoize</span>(<span class="ruby-value">:to_rankine</span>, <span class="ruby-operator">-></span> {
|
1144
|
+
<span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span> <span class="ruby-operator">==</span> <span class="ruby-constant">RANKINE</span>
|
1145
|
+
|
1146
|
+
<span class="ruby-identifier">degrees</span> =
|
1147
|
+
<span class="ruby-keyword">case</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">scale</span>
|
1148
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">CELSIUS</span>
|
1149
|
+
(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">+</span> <span class="ruby-value">273.15</span>) <span class="ruby-operator">*</span> (<span class="ruby-value">9</span> <span class="ruby-operator">/</span> <span class="ruby-value">5r</span>)
|
1150
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">FAHRENHEIT</span>
|
1151
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">+</span> <span class="ruby-value">459.67</span>
|
1152
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">KELVIN</span>
|
1153
|
+
<span class="ruby-keyword">self</span>.<span class="ruby-identifier">degrees</span> <span class="ruby-operator">*</span> (<span class="ruby-value">9</span> <span class="ruby-operator">/</span> <span class="ruby-value">5r</span>)
|
1154
|
+
<span class="ruby-keyword">end</span>
|
1155
|
+
|
1156
|
+
<span class="ruby-constant">BasicTemperature</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">degrees</span>, <span class="ruby-constant">RANKINE</span>)
|
1157
|
+
})
|
1158
|
+
<span class="ruby-keyword">end</span></pre>
|
1159
|
+
</div>
|
1160
|
+
</div>
|
1161
|
+
|
1162
|
+
</div>
|
1163
|
+
|
1164
|
+
<div class="method">
|
1165
|
+
<div class="title method-title" id="method-i-to_scale">
|
1166
|
+
|
1167
|
+
<b>to_scale</b>(scale)
|
1168
|
+
|
1169
|
+
<a href="../classes/BasicTemperature.html#method-i-to_scale" name="method-i-to_scale" class="permalink">Link</a>
|
1170
|
+
</div>
|
1171
|
+
|
1172
|
+
|
1173
|
+
<div class="description">
|
1174
|
+
<p>Converts temperature to specific <code>scale</code>. If temperature is already in desired <code>scale</code>, returns current temperature object.</p>
|
1175
|
+
|
1176
|
+
<p>Raises <a href="BasicTemperature/InvalidScaleError.html">InvalidScaleError</a> when <code>scale</code> can not be casted to any possible scale value (see <a href="BasicTemperature.html#SCALES">SCALES</a>).</p>
|
1177
|
+
|
1178
|
+
<pre><code>Temperature[60, :fahrenheit].to_scale(:celsius)
|
1179
|
+
# => 15.56 °C
|
1180
|
+
</code></pre>
|
1181
|
+
</div>
|
1182
|
+
|
1183
|
+
|
1184
|
+
|
1185
|
+
|
1186
|
+
|
1187
|
+
|
1188
|
+
|
1189
|
+
|
1190
|
+
<div class="sourcecode">
|
1191
|
+
|
1192
|
+
<p class="source-link">
|
1193
|
+
Source:
|
1194
|
+
<a href="javascript:toggleSource('method-i-to_scale_source')" id="l_method-i-to_scale_source">show</a>
|
1195
|
+
|
1196
|
+
</p>
|
1197
|
+
<div id="method-i-to_scale_source" class="dyn-source">
|
1198
|
+
<pre><span class="ruby-comment"># File lib/basic_temperature.rb, line 277</span>
|
1199
|
+
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_scale</span>(<span class="ruby-identifier">scale</span>)
|
1200
|
+
<span class="ruby-identifier">casted_scale</span> = <span class="ruby-identifier">cast_scale</span>(<span class="ruby-identifier">scale</span>)
|
1201
|
+
|
1202
|
+
<span class="ruby-identifier">assert_valid_scale!</span>(<span class="ruby-identifier">casted_scale</span>)
|
1203
|
+
|
1204
|
+
<span class="ruby-keyword">case</span> <span class="ruby-identifier">casted_scale</span>
|
1205
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">CELSIUS</span>
|
1206
|
+
<span class="ruby-identifier">to_celsius</span>
|
1207
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">FAHRENHEIT</span>
|
1208
|
+
<span class="ruby-identifier">to_fahrenheit</span>
|
1209
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">KELVIN</span>
|
1210
|
+
<span class="ruby-identifier">to_kelvin</span>
|
1211
|
+
<span class="ruby-keyword">when</span> <span class="ruby-constant">RANKINE</span>
|
1212
|
+
<span class="ruby-identifier">to_rankine</span>
|
1213
|
+
<span class="ruby-keyword">end</span>
|
1214
|
+
<span class="ruby-keyword">end</span></pre>
|
1215
|
+
</div>
|
1216
|
+
</div>
|
1217
|
+
|
1218
|
+
</div>
|
1219
|
+
|
1220
|
+
|
1221
|
+
|
1222
|
+
|
1223
|
+
</div>
|
1224
|
+
|
1225
|
+
</div>
|
1226
|
+
</body>
|
1227
|
+
</html>
|