metric_conversions 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
19
+ lib/.DS_Store
20
+ tests/.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in metric_conversions.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 profh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # MetricConversions
2
+ ==========
3
+ These are a set of methods for length, volume, temperature, area and the like that help a user convert to or from English units to Metric units. This was done as a class exercise in 67-275 at Carnegie Mellon University, with a pair of students tackling each of the submodules and writing the necessary tests.
4
+
5
+
6
+ Installation
7
+ ------------
8
+ Installing this gem is pretty simple -- just type on the command line:
9
+
10
+ ```
11
+ $ gem install metric_conversions
12
+ ```
13
+
14
+ And add this gem into any other code with:
15
+
16
+ ```
17
+ require 'rubygems'
18
+ require 'metric_conversions'
19
+ ```
20
+
21
+
22
+ Usage
23
+ ------------
24
+ There are currently four submodules to this gem:
25
+
26
+ * Lengths
27
+ * Volumes
28
+ * Temperatures
29
+ * Areas
30
+
31
+
32
+ ### Lengths ###
33
+ This gem enables you to convert between typical metric and US Standard units. By calling .convert on a number and passing in two arguments, units from and units to, you can convert the given number into its equivalent in the "to" units.
34
+
35
+ Example:
36
+ 250.convert("in", "cm")
37
+
38
+ This module was built by Rachel Crown and Dylan Corwin.
39
+
40
+
41
+ ### Volumes ###
42
+ ... notes and explanation goes here ...
43
+
44
+ This module was built by <YOUR NAME HERE> and <YOUR NAME HERE>.
45
+
46
+
47
+ ### Temperatures ###
48
+ ... notes and explanation goes here ...
49
+
50
+ This module was built by <YOUR NAME HERE> and <YOUR NAME HERE>.
51
+
52
+
53
+ ### Areas ###
54
+ ... notes and explanation goes here ...
55
+
56
+ This module was built by <YOUR NAME HERE> and <YOUR NAME HERE>.
57
+
58
+
59
+ ### Note on tests ###
60
+
61
+ Each submodule of the gem has a set of basic tests associated with it. The tests provide other examples of what is possible using this methods and may help the user further understand how this gem can be applied. The testing suite requires the minitest gem to run properly. Matt Sears has a nice quick reference guide for using minitest at: [http://mattsears.com/articles/2011/12/10/minitest-quick-reference](http://mattsears.com/articles/2011/12/10/minitest-quick-reference)
62
+
63
+
64
+ Contributing
65
+ ------------
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module MetricConversions
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,165 @@
1
+ module MetricConversions
2
+
3
+ # rounds a number to 2 digits
4
+ def round_2
5
+ (self * 100).round / 100.0
6
+ end
7
+
8
+ # determines if a given unit is metric
9
+ def is_metric?(unit)
10
+ return (unit == "mm" || unit == "cm" || unit == "m" || unit == "km")
11
+ end
12
+
13
+ # performs the actual unit length conversion given a value
14
+ def convert(from, to)
15
+ # if we are converting from a metric value
16
+ if(is_metric?(from))
17
+ # because from is metric, convert to centimeters
18
+ cm = self.to_cm(from)
19
+
20
+ # if we are converting to a metric value, convert the value
21
+ if(is_metric?(to))
22
+ if(to == "mm")
23
+ cm.to_mm.round_2
24
+
25
+ elsif(to == "cm")
26
+ cm.round_2
27
+
28
+ elsif(to == "m")
29
+ cm.to_m.round_2
30
+
31
+ elsif(to == "km")
32
+ cm.to_km.round_2
33
+ end
34
+
35
+ # the value we are converting to is not metric, so convert the value to inches
36
+ else
37
+ inches = cm.to_inches("cm")
38
+
39
+ # if we are converting to English values, perform the conversion
40
+ if(to == "in")
41
+ inches.round_2
42
+
43
+ elsif(to == "ft")
44
+ inches.to_ft.round_2
45
+
46
+ elsif(to == "yd")
47
+ inches.to_yd.round_2
48
+
49
+ elsif(to == "mi")
50
+ inches.to_mi.round_2
51
+ end
52
+ end
53
+
54
+ else
55
+ # the value we are converting from is not metric
56
+ inches = self.to_inches(from)
57
+
58
+ # if the value we are converting to is metric, convert inches to centimeters and make the final conversion
59
+ if(is_metric?(to))
60
+ cm = inches.to_cm("in")
61
+
62
+ if(to == "mm")
63
+ cm.to_mm.round_2
64
+
65
+ elsif(to == "cm")
66
+ cm.round_2
67
+
68
+ elsif(to == "m")
69
+ cm.to_m.round_2
70
+
71
+ elsif(to == "km")
72
+ cm.to_km.round_2
73
+ end
74
+
75
+ # the value we are converting to is in the English system, so perform the conversion
76
+ else
77
+ if(to == "in")
78
+ inches.round_2
79
+
80
+ elsif(to == "ft")
81
+ inches.to_ft.round_2
82
+
83
+ elsif(to == "yd")
84
+ inches.to_yd.round_2
85
+
86
+ elsif(to == "mi")
87
+ inches.to_mi.round_2
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ # given a from value, convert it to inches
94
+ def to_inches(from)
95
+ if(from == "ft")
96
+ self * 12.0
97
+
98
+ elsif(from == "yd")
99
+ self * 36.0
100
+
101
+ elsif(from == "mi")
102
+ self * 12.0 * 5280
103
+
104
+ elsif(from == "cm")
105
+ self / 2.54
106
+
107
+ elsif(from == "in")
108
+ self * 1.0
109
+
110
+ end
111
+ end
112
+
113
+ # given a from value, convert it to centimeters
114
+ def to_cm(from)
115
+ if(from == "mm")
116
+ self / 10.0
117
+
118
+ elsif(from == "cm")
119
+ self * 1.0
120
+
121
+ elsif(from == "m")
122
+ self * 100.0
123
+
124
+ elsif(from == "in")
125
+ self * 2.54
126
+
127
+ else
128
+ self * 100000.0
129
+
130
+ end
131
+ end
132
+
133
+ # given inches, convert to feet
134
+ def to_ft
135
+ self / 12.0
136
+ end
137
+
138
+ # given inches, convert to yards
139
+ def to_yd
140
+ self.to_ft / 3.0
141
+ end
142
+
143
+ # given inches, convert to miles
144
+ def to_mi
145
+ self.to_ft / 5280.0
146
+ end
147
+
148
+ # given centimeters, convert to millimeters
149
+ def to_mm
150
+ self * 10.0
151
+ end
152
+
153
+ # given centimeters, convert to meters
154
+ def to_m
155
+ self / 100.0
156
+ end
157
+
158
+ # given centimeters, convert to kilometers
159
+ def to_km
160
+ self / 100000.0
161
+ end
162
+ end
163
+
164
+ (2.class).send(:include, MetricConversions)
165
+ ((2.54).class).send(:include, MetricConversions)
@@ -0,0 +1,3 @@
1
+ module MetricConversions
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,3 @@
1
+ module MetricConversions
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ module MetricConversions
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,8 @@
1
+ # Provided by bundler to track the version
2
+ require "metric_conversions/version"
3
+
4
+ # Our code broken into submodules for each team to work on
5
+ require "metric_conversions/lengths"
6
+ require "metric_conversions/volumes"
7
+ require "metric_conversions/temperatures"
8
+ require "metric_conversions/areas"
@@ -0,0 +1,54 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require '../metric_conversions/lengths.rb'
4
+
5
+ describe "lengths conversions" do
6
+ it "should return in inches the number initially given in feet" do
7
+ assert_equal(12, 1.convert("ft", "in"))
8
+ assert_equal(18, 1.5.convert("ft", "in"))
9
+ end
10
+
11
+ it "should return in centimeters the number initially given in inches" do
12
+ assert_equal(2.54, 1.convert("in", "cm"))
13
+ assert_equal(635, 250.convert("in", "cm"))
14
+ end
15
+
16
+ it "should return in inches the number initially given in centimeters" do
17
+ assert_equal(1, 2.54.convert("cm", "in"))
18
+ assert_equal(196.85, 500.convert("cm", "in"))
19
+ end
20
+
21
+ it "should return in cm the number initially given in feet" do
22
+ assert_equal(91.44, 3.convert("ft", "cm"))
23
+ assert_equal(60.96, 2.convert("ft", "cm"))
24
+ end
25
+
26
+ it "should return in cm the number initially given in miles" do
27
+ assert_equal(321868.8, 2.convert("mi", "cm"))
28
+ end
29
+
30
+ it "should return in mm the number initially given in ft" do
31
+ assert_equal(609.6, 2.convert("ft", "mm"))
32
+ end
33
+
34
+ it "should return in meters the number initially given in yards" do
35
+ assert_equal(1.83, 2.convert("yd", "m"))
36
+ end
37
+
38
+ it "should return in km the number initially given in miles" do
39
+ assert_equal(3.22, 2.convert("mi", "km"))
40
+ end
41
+
42
+ it "should return in yards the number initially given in km" do
43
+ assert_equal(2187.23, 2.convert("km", "yd"))
44
+ end
45
+
46
+ it "should return in ft the number initially given in m" do
47
+ assert_equal(6.56, 2.convert("m", "ft"))
48
+ end
49
+
50
+ it "should return in inches the number initially given in mm" do
51
+ assert_equal(17.01, 432.convert("mm", "in"))
52
+ end
53
+
54
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/metric_conversions/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["profh and class"]
6
+ gem.email = ["profh@cmu.edu"]
7
+ gem.description = %q{A set of methods to help make conversions to/from Metric system and English system of measurement}
8
+ gem.summary = %q{This was initially done as a class project for 67-275 at Carnegie Mellon University}
9
+ gem.homepage = "https://github.com/profh/metric_conversions"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "metric_conversions"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MetricConversions::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metric_conversions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - profh and class
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A set of methods to help make conversions to/from Metric system and English
15
+ system of measurement
16
+ email:
17
+ - profh@cmu.edu
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/metric_conversions.rb
28
+ - lib/metric_conversions/areas.rb
29
+ - lib/metric_conversions/lengths.rb
30
+ - lib/metric_conversions/temperatures.rb
31
+ - lib/metric_conversions/version.rb
32
+ - lib/metric_conversions/volumes.rb
33
+ - lib/metric_conversions_tests/lengths_test.rb
34
+ - metric_conversions.gemspec
35
+ homepage: https://github.com/profh/metric_conversions
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: This was initially done as a class project for 67-275 at Carnegie Mellon
59
+ University
60
+ test_files: []