joules 0.3.0 → 0.3.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.
@@ -3,257 +3,264 @@
3
3
  #
4
4
  # @description: Module for providing electricity formulas
5
5
  # @author: Elisha Lai
6
- # @version: 0.3.0 29/05/2015
6
+ # @version: 0.3.1 01/06/2015
7
7
  #==============================================================================
8
8
 
9
9
  # Electricity module (electricity.rb)
10
10
 
11
- # Calculates the current given charge and time.
12
- # @param charge [Int, Float]
13
- # charge is in coulombs
14
- # @param time [Int, Float]
15
- # time > 0; time is in seconds
16
- # @return [Float]
17
- # return value is in amperes
18
- # @example
19
- # current(325, 5) #=> 65.0
20
- def current(charge, time)
21
- return charge / time.to_f
22
- end
11
+ module Joules
12
+ module_function
23
13
 
24
- # Calculates the resistance given voltage and current.
25
- # @param voltage [Int, Float]
26
- # voltage is in volts
27
- # @param current [Int, Float]
28
- # current != 0; current is in amperes
29
- # @return [Float]
30
- # return value is in ohms
31
- # @example
32
- # resistance(1.8, 0.6) #=> 3.0
33
- # @note There is one other method for calculating resistance.
34
- def resistance_v1(voltage, current)
35
- return voltage / current.to_f
36
- end
14
+ # Calculates the current given charge and time.
15
+ # @param charge [Int, Float]
16
+ # charge is in coulombs
17
+ # @param time [Int, Float]
18
+ # time > 0; time is in seconds
19
+ # @return [Float]
20
+ # return value is in amperes
21
+ # @example
22
+ # Joules.current(325, 5) #=> 65.0
23
+ def current(charge, time)
24
+ return charge / time.to_f
25
+ end
37
26
 
38
- # Calculates the resistance given resistivity, wire length, and cross sectional area.
39
- # @param resistivity [Int, Float]
40
- # resistivity >= 0; resistivity is in ohm metres
41
- # @param wire_length [Int, Float]
42
- # wire_length >= 0; wire_length is in meters
43
- # @param cross_sectional_area [Int, Float]
44
- # cross_sectional_area > 0; cross_sectional_area is in meters squared
45
- # @return [Float]
46
- # return value >= 0; return value is in ohms
47
- # @example
48
- # resistance_v2(1e13, 250, 0.4) #=> 6.25e+15
49
- # @note There is one other method for calculating resistance.
50
- def resistance_v2(resistivity, wire_length, cross_sectional_area)
51
- return (resistivity * wire_length) / cross_sectional_area.to_f
52
- end
27
+ # Calculates the resistance given voltage and current.
28
+ # @param voltage [Int, Float]
29
+ # voltage is in volts
30
+ # @param current [Int, Float]
31
+ # current != 0; current is in amperes
32
+ # @return [Float]
33
+ # return value is in ohms
34
+ # @example
35
+ # Joules.resistance_v1(1.8, 0.6) #=> 3.0
36
+ # @note There is one other method for calculating resistance.
37
+ def resistance_v1(voltage, current)
38
+ return voltage / current.to_f
39
+ end
53
40
 
54
- # Calculates the total resistance of resistors in series.
55
- # @param resistances [Array<Int, Float>]
56
- # each resistance in resistances is in ohms
57
- # @return [Float]
58
- # return value is in ohms
59
- # @example
60
- # resistance_in_series([10, 5, 3.4, 6.3]) #=> 24.7
61
- def resistance_in_series(resistances)
62
- total_resistance = 0
63
- resistances.each do |resistance|
64
- total_resistance += resistance
41
+ # Calculates the resistance given resistivity, wire length, and cross sectional area.
42
+ # @param resistivity [Int, Float]
43
+ # resistivity >= 0; resistivity is in ohm metres
44
+ # @param wire_length [Int, Float]
45
+ # wire_length >= 0; wire_length is in meters
46
+ # @param cross_sectional_area [Int, Float]
47
+ # cross_sectional_area > 0; cross_sectional_area is in meters squared
48
+ # @return [Float]
49
+ # return value >= 0; return value is in ohms
50
+ # @example
51
+ # Joules.resistance_v2(1e13, 250, 0.4) #=> 6.25e+15
52
+ # @note There is one other method for calculating resistance.
53
+ def resistance_v2(resistivity, wire_length, cross_sectional_area)
54
+ return (resistivity * wire_length) / cross_sectional_area.to_f
65
55
  end
66
- return total_resistance.to_f
67
- end
68
56
 
69
- # Calculates the total resistance of resistors in parallel.
70
- # @param resistances [Array<Int, Float>]
71
- # each resistance in resistances != 0; each resistance in resistances is in ohms
72
- # @return [Float]
73
- # return value is in ohms
74
- # @example
75
- # resistance_in_parallel([0.5, 0.25, 0.125]) #=> 0.07142857142857142
76
- def resistance_in_parallel(resistances)
77
- if resistances.empty?
78
- raise ArgumentError.new('empty list of resistances')
57
+ # Calculates the total resistance of resistors in series.
58
+ # @param resistances [Array<Int, Float>]
59
+ # each resistance in resistances is in ohms
60
+ # @return [Float]
61
+ # return value is in ohms
62
+ # @example
63
+ # Joules.resistance_in_series([10, 5, 3.4, 6.3]) #=> 24.7
64
+ def resistance_in_series(resistances)
65
+ total_resistance = 0
66
+ resistances.each do |resistance|
67
+ total_resistance += resistance
68
+ end
69
+ return total_resistance.to_f
79
70
  end
80
- total_resistance = 0
81
- resistances.each do |resistance|
82
- total_resistance += (1.0 / resistance)
71
+
72
+ # Calculates the total resistance of resistors in parallel.
73
+ # @param resistances [Array<Int, Float>]
74
+ # each resistance in resistances != 0; each resistance in resistances is in ohms
75
+ # @return [Float]
76
+ # return value is in ohms
77
+ # @example
78
+ # Joules.resistance_in_parallel([0.5, 0.25, 0.125]) #=> 0.07142857142857142
79
+ def resistance_in_parallel(resistances)
80
+ total_resistance = 0
81
+ if resistances.empty?
82
+ return total_resistance.to_f
83
+ else
84
+ resistances.each do |resistance|
85
+ total_resistance += (1.0 / resistance)
86
+ end
87
+ return 1 / total_resistance
88
+ end
83
89
  end
84
- return 1 / total_resistance
85
- end
86
90
 
87
- # Calculates the total capacitance given charge and voltage.
88
- # @param charge [Int, Float]
89
- # charge is in coulombs
90
- # @param voltage [Int, Float]
91
- # voltage != 0; voltage is in volts
92
- # @return [Float]
93
- # return value is in farads
94
- # @example
95
- # capacitance(2e-3, 100) #=> 2.0e-05
96
- def capacitance(charge, voltage)
97
- return charge / voltage.to_f
98
- end
91
+ # Calculates the total capacitance given charge and voltage.
92
+ # @param charge [Int, Float]
93
+ # charge is in coulombs
94
+ # @param voltage [Int, Float]
95
+ # voltage != 0; voltage is in volts
96
+ # @return [Float]
97
+ # return value is in farads
98
+ # @example
99
+ # Joules.capacitance(2e-3, 100) #=> 2.0e-05
100
+ def capacitance(charge, voltage)
101
+ return charge / voltage.to_f
102
+ end
99
103
 
100
- # Calculates the capacitor potential energy given charge and voltage.
101
- # @param charge [Int, Float]
102
- # charge is in coulombs
103
- # @param voltage [Int, Float]
104
- # voltage is in volts
105
- # @return [Float]
106
- # return value is in joules
107
- # @example
108
- # capacitor_potential_energy_v1(1.5, 30) #=> 22.5
109
- # @note There are two other methods for calculating capacitor potential energy.
110
- def capacitor_potential_energy_v1(charge, voltage)
111
- return 0.5 * charge * voltage
112
- end
104
+ # Calculates the capacitor potential energy given charge and voltage.
105
+ # @param charge [Int, Float]
106
+ # charge is in coulombs
107
+ # @param voltage [Int, Float]
108
+ # voltage is in volts
109
+ # @return [Float]
110
+ # return value is in joules
111
+ # @example
112
+ # Joules.capacitor_potential_energy_v1(1.5, 30) #=> 22.5
113
+ # @note There are two other methods for calculating capacitor potential energy.
114
+ def capacitor_potential_energy_v1(charge, voltage)
115
+ return 0.5 * charge * voltage
116
+ end
113
117
 
114
- # Calculates the capacitor potential energy given capacitance and voltage.
115
- # @param capacitance [Int, Float]
116
- # capacitance is in farads
117
- # @param voltage [Int, Float]
118
- # voltage is in volts
119
- # @return [Float]
120
- # return value is in joules
121
- # @example
122
- # capacitor_potential_energy_v2(10e-6, 20) #=> 0.002
123
- # @note There are two other methods for calculating capacitor potential energy.
124
- def capacitor_potential_energy_v2(capacitance, voltage)
125
- return 0.5 * capacitance * (voltage ** 2)
126
- end
118
+ # Calculates the capacitor potential energy given capacitance and voltage.
119
+ # @param capacitance [Int, Float]
120
+ # capacitance is in farads
121
+ # @param voltage [Int, Float]
122
+ # voltage is in volts
123
+ # @return [Float]
124
+ # return value is in joules
125
+ # @example
126
+ # Joules.capacitor_potential_energy_v2(10e-6, 20) #=> 0.002
127
+ # @note There are two other methods for calculating capacitor potential energy.
128
+ def capacitor_potential_energy_v2(capacitance, voltage)
129
+ return 0.5 * capacitance * (voltage ** 2)
130
+ end
127
131
 
128
- # Calculates the capacitor potential energy given charge and capacitance.
129
- # @param charge [Int, Float]
130
- # charge is in coulombs
131
- # @param capacitance [Int, Float]
132
- # capacitance is in farads
133
- # @return [Float]
134
- # return value is in joules
135
- # @example
136
- # capacitor_potential_energy_v3(25, 50) #=> 6.25
137
- # @note There are two other methods for calculating capacitor potential energy.
138
- def capacitor_potential_energy_v3(charge, capacitance)
139
- return (0.5 * (charge ** 2)) / capacitance
140
- end
132
+ # Calculates the capacitor potential energy given charge and capacitance.
133
+ # @param charge [Int, Float]
134
+ # charge is in coulombs
135
+ # @param capacitance [Int, Float]
136
+ # capacitance is in farads
137
+ # @return [Float]
138
+ # return value is in joules
139
+ # @example
140
+ # Joules.capacitor_potential_energy_v3(25, 50) #=> 6.25
141
+ # @note There are two other methods for calculating capacitor potential energy.
142
+ def capacitor_potential_energy_v3(charge, capacitance)
143
+ return (0.5 * (charge ** 2)) / capacitance
144
+ end
141
145
 
142
- # Calculates the total capacitance of capacitors in series.
143
- # @param capacitances [Array<Int, Float>]
144
- # each capacitance in resistances != 0; each capacitance in capacitances is in farads
145
- # @return [Float]
146
- # return value is in farads
147
- # @example
148
- # capacitance_in_series([0.5, 0.25, 0.125]) #=> 0.07142857142857142
149
- def capacitance_in_series(capacitances)
150
- if capacitances.empty?
151
- raise ArgumentError.new('empty list of capacitances')
146
+ # Calculates the total capacitance of capacitors in series.
147
+ # @param capacitances [Array<Int, Float>]
148
+ # each capacitance in resistances != 0; each capacitance in capacitances is in farads
149
+ # @return [Float]
150
+ # return value is in farads
151
+ # @example
152
+ # Joules.capacitance_in_series([0.5, 0.25, 0.125]) #=> 0.07142857142857142
153
+ def capacitance_in_series(capacitances)
154
+ total_capacitance = 0
155
+ if capacitances.empty?
156
+ return total_capacitance.to_f
157
+ else
158
+ capacitances.each do |capacitance|
159
+ total_capacitance += (1.0 / capacitance)
160
+ end
161
+ return 1 / total_capacitance
162
+ end
152
163
  end
153
- total_capacitance = 0
154
- capacitances.each do |capacitance|
155
- total_capacitance += (1.0 / capacitance)
164
+
165
+ # Calculates the total capacitance of capacitors in parallel.
166
+ # @param capacitances [Array<Int, Float>]
167
+ # each capacitance in capacitances is in farads
168
+ # @return [Float]
169
+ # return value is in farads
170
+ # @example
171
+ # Joules.capacitance_in_parallel([10, 5, 3.4, 6.3]) #=> 24.7
172
+ def capacitance_in_parallel(capacitances)
173
+ total_capacitance = 0
174
+ capacitances.each do |capacitance|
175
+ total_capacitance += capacitance
176
+ end
177
+ return total_capacitance.to_f
156
178
  end
157
- return 1 / total_capacitance
158
- end
159
179
 
160
- # Calculates the total capacitance in parallel given capacitances.
161
- # @param capacitances [Array<Int, Float>]
162
- # each capacitance in capacitances is in farads
163
- # @return [Float]
164
- # return value is in farads
165
- # @example
166
- # capacitance_in_parallel([10, 5, 3.4, 6.3]) #=> 24.7
167
- def capacitance_in_parallel(capacitances)
168
- total_capacitance = 0
169
- capacitances.each do |capacitance|
170
- total_capacitance += capacitance
180
+ # Calculates the voltage given energy and charge.
181
+ # @param energy [Int, Float]
182
+ # energy is in joules
183
+ # @param charge [Int, Float]
184
+ # charge != 0; charge is in coulombs
185
+ # @return [Float]
186
+ # return value is in volts
187
+ # @example
188
+ # Joules.voltage_v1(1.8, 0.6) #=> 3.0
189
+ # @note There is one other method for calculating voltage.
190
+ def voltage_v1(energy, charge)
191
+ return energy / charge.to_f
171
192
  end
172
- return total_capacitance.to_f
173
- end
174
193
 
175
- # Calculates the voltage given energy and charge.
176
- # @param energy [Int, Float]
177
- # energy is in joules
178
- # @param charge [Int, Float]
179
- # charge != 0; charge is in coulombs
180
- # @return [Float]
181
- # return value is in volts
182
- # @example
183
- # voltage_v1(1.8, 0.6) #=> 3.0
184
- # @note There is one other method for calculating voltage.
185
- def voltage_v1(energy, charge)
186
- return energy / charge.to_f
187
- end
194
+ # Calculates the voltage given current and resistance.
195
+ # @param current [Int, Float]
196
+ # current is in amperes
197
+ # @param resistance [Int, Float]
198
+ # resistance is in ohms
199
+ # @return [Float]
200
+ # return value is in volts
201
+ # @example
202
+ # Joules.voltage_v2(0.6, 3) #=> 1.8
203
+ # @note There is one other method for calculating voltage.
204
+ def voltage_v2(current, resistance)
205
+ return current * resistance.to_f
206
+ end
188
207
 
189
- # Calculates the voltage given current and resistance.
190
- # @param current [Int, Float]
191
- # current is in amperes
192
- # @param resistance [Int, Float]
193
- # resistance is in ohms
194
- # @return [Float]
195
- # return value is in volts
196
- # @example
197
- # voltage_v2(0.6, 3) #=> 1.8
198
- # @note There is one other method for calculating voltage.
199
- def voltage_v2(current, resistance)
200
- return current * resistance.to_f
201
- end
208
+ # Calculates the power given voltage and current.
209
+ # @param voltage [Int, Float]
210
+ # voltage is in volts
211
+ # @param current [Int, Float]
212
+ # current is in amperes
213
+ # @return [Float]
214
+ # return value is in watts
215
+ # @example
216
+ # Joules.power_v3(1.8, 0.6) #=> 1.08
217
+ # @note There are four other methods for calculating power.
218
+ def power_v3(voltage, current)
219
+ return voltage * current.to_f
220
+ end
202
221
 
203
- # Calculates the power given voltage and current.
204
- # @param voltage [Int, Float]
205
- # voltage is in volts
206
- # @param current [Int, Float]
207
- # current is in amperes
208
- # @return [Float]
209
- # return value is in watts
210
- # @example
211
- # power_v3(1.8, 0.6) #=> 1.08
212
- # @note There are four other methods for calculating power.
213
- def power_v3(voltage, current)
214
- return voltage * current.to_f
215
- end
222
+ # Calculates the power given current and resistance.
223
+ # @param current [Int, Float]
224
+ # current is in amperes
225
+ # @param resistance [Int, Float]
226
+ # resistance is in ohms
227
+ # @return [Float]
228
+ # return value is in watts
229
+ # @example
230
+ # Joules.power_v4(0.6, 3) #=> 1.08
231
+ # @note There are four other methods for calculating power.
232
+ def power_v4(current, resistance)
233
+ return (current ** 2.0) * resistance
234
+ end
216
235
 
217
- # Calculates the power given current and resistance.
218
- # @param current [Int, Float]
219
- # current is in amperes
220
- # @param resistance [Int, Float]
221
- # resistance is in ohms
222
- # @return [Float]
223
- # return value is in watts
224
- # @example
225
- # power_v4(0.6, 3) #=> 1.08
226
- # @note There are four other methods for calculating power.
227
- def power_v4(current, resistance)
228
- return (current ** 2.0) * resistance
229
- end
236
+ # Calculates the power given voltage and resistance.
237
+ # @param voltage [Int, Float]
238
+ # voltage is in volts
239
+ # @param resistance [Int, Float]
240
+ # resistance != 0; resistance is in ohms
241
+ # @return [Float]
242
+ # return value is in watts
243
+ # @example
244
+ # Joules.power_v5(1.8, 3) #=> 1.08
245
+ # @note There are four other methods for calculating power.
246
+ def power_v5(voltage, resistance)
247
+ return (voltage ** 2.0) / resistance
248
+ end
230
249
 
231
- # Calculates the power given voltage and resistance.
232
- # @param voltage [Int, Float]
233
- # voltage is in volts
234
- # @param resistance [Int, Float]
235
- # resistance != 0; resistance is in ohms
236
- # @return [Float]
237
- # return value is in watts
238
- # @example
239
- # power_v5(1.8, 3) #=> 1.08
240
- # @note There are four other methods for calculating power.
241
- def power_v5(voltage, resistance)
242
- return (voltage ** 2.0) / resistance
243
- end
250
+ # Calculates the energy given voltage, current, and time.
251
+ # @param voltage [Int, Float]
252
+ # voltage is in volts
253
+ # @param current [Int, Float]
254
+ # current is in amperes
255
+ # @param time [Int, Float]
256
+ # time >= 0; time is in seconds
257
+ # @return [Float]
258
+ # return value is in joules
259
+ # @example
260
+ # Joules.energy_v3(1.8, 0.6, 5) #=> 5.4
261
+ # @note There are two other methods for calculating energy.
262
+ def energy_v3(voltage, current, time)
263
+ return power_v2(voltage, current) * time
264
+ end
244
265
 
245
- # Calculates the energy given voltage, current, and time.
246
- # @param voltage [Int, Float]
247
- # voltage is in volts
248
- # @param current [Int, Float]
249
- # current is in amperes
250
- # @param time [Int, Float]
251
- # time >= 0; time is in seconds
252
- # @return [Float]
253
- # return value is in joules
254
- # @example
255
- # energy_v3(1.8, 0.6, 5) #=> 5.4
256
- # @note There are two other methods for calculating energy.
257
- def energy_v3(voltage, current, time)
258
- return power_v2(voltage, current) * time
259
266
  end