change_final 0.0.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/change_final.rb +46 -0
  3. metadata +42 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 800f9fe5d9f70fbd84f5fa5be0db95714ca919900dfd8731179a62be0902c4ac
4
+ data.tar.gz: f225c9a03399410e29995b3d2fbf1f12a9a48eee5bdb5125a5bdf9dc22d99037
5
+ SHA512:
6
+ metadata.gz: 64363875c625109dd033b7453f0eba6809483b11dd39a89c06a30daaa3a521d045bed5e3609f9c10b66c9dbd68586d07e2fe81b8355e051be3fdede5644a40ee
7
+ data.tar.gz: 0b716cb9aa15094352cb18f4a36abdb83451986dc3e30c4205e380c8aef242f1cc6e4341b6fde3cfdea597a25051ae66567d50ffd7bde37582d86815bee5c245
@@ -0,0 +1,46 @@
1
+ require 'singleton'
2
+ module ChangeFinal
3
+
4
+ class ConversionError < StandardError
5
+ end
6
+ class Different
7
+ include Singleton
8
+ # lookup table
9
+ CONVERSIONS = {
10
+ kg: {
11
+ pounds: 2.20462,
12
+ stone: 0.157473,
13
+ },
14
+ pounds: {
15
+ kg: 0.453592,
16
+ stone: 0.0714286
17
+ },
18
+ stone: {
19
+ kg: 6.35029,
20
+ pounds: 14
21
+ }
22
+ }
23
+ weight = Different.instance
24
+
25
+ def convert(value, from, to:)
26
+ raise ConversionError, "Value is not numeric" unless value.is_a? Numeric
27
+ raise ConversionError, "Value #{value} is not positive" unless value >= 0
28
+ "#{value * conversion_ratio(from, to) }#{to.to_s}"
29
+ end
30
+
31
+ def conversions(value, unit)
32
+ CONVERSIONS[unit].keys.map do |other_unit|
33
+ convert(value, unit, to: other_unit)
34
+ end
35
+ end
36
+
37
+ private
38
+ def conversion_ratio(from, to)
39
+ begin
40
+ CONVERSIONS.fetch(from).fetch(to)
41
+ rescue KeyError
42
+ raise ConversionError, "Cannot convert #{from.to_s} to #{to.to_s}"
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,42 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: change_final
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gavin Smyth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem
14
+ email: gavinsmyth1998@gmail.ie
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/change_final.rb
20
+ homepage:
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubygems_version: 3.0.3
39
+ signing_key:
40
+ specification_version: 4
41
+ summary: change weight!
42
+ test_files: []