resistor 0.2.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Guardfile +9 -0
- data/README.md +7 -6
- data/lib/resistor.rb +2 -2
- data/lib/resistor/basic_resistor.rb +25 -21
- data/lib/resistor/color_code.rb +13 -16
- data/lib/resistor/combined_resistor.rb +2 -0
- data/lib/resistor/version.rb +1 -1
- data/spec/basic_resistor_spec.rb +53 -40
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ba5f3aa26c28e62800c94ee3bea8eab62b9295b
|
4
|
+
data.tar.gz: 78e020b33d5eaed94e1956a0732626c16189e510
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d45490081759694cdd282f3bcc59c00777d6c512149efac8ef79806c606ca461008ef541c178133bf527b279a5dedca60a2a3f0203af2047bc49730ed2aa0c4
|
7
|
+
data.tar.gz: 8b952ccbbe8da1a138ed560f8a4e0a1719f09e651f39881d9fd86fda3c91ccda5f91c5e85da785bc7a487d319af32095006c8a682c2244df1c6345390d70a890
|
data/Gemfile
CHANGED
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Resistor
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/resistor.svg)](http://badge.fury.io/rb/resistor)
|
2
3
|
Resistor is a gem for the resistor unit.
|
3
4
|
|
4
5
|
## Installation
|
@@ -41,10 +42,10 @@ Resistor::ColorCode.decode([:yellow, :purple, :red, :gold])
|
|
41
42
|
```ruby
|
42
43
|
require 'resistor'
|
43
44
|
|
44
|
-
r1 = Resistor.new(
|
45
|
-
r2 = Resistor.new(
|
46
|
-
r3 = Resistor.new(
|
47
|
-
r4 = Resistor.new(
|
45
|
+
r1 = Resistor.new(20)
|
46
|
+
r2 = Resistor.new(30)
|
47
|
+
r3 = Resistor.new(4)
|
48
|
+
r4 = Resistor.new(8)
|
48
49
|
|
49
50
|
# --[r1]-- --[r4]--
|
50
51
|
# --| |--[r3]--| |--
|
@@ -60,8 +61,8 @@ r5.ohm # => 20.0
|
|
60
61
|
```ruby
|
61
62
|
require 'resistor'
|
62
63
|
|
63
|
-
r1 = Resistor.new(
|
64
|
-
r2 = Resistor.new(
|
64
|
+
r1 = Resistor.new(4700)
|
65
|
+
r2 = Resistor.new(62)
|
65
66
|
|
66
67
|
r1.e12? # => true
|
67
68
|
r1.e24? # => true
|
data/lib/resistor.rb
CHANGED
@@ -13,8 +13,8 @@ module Resistor
|
|
13
13
|
# @example Create a Resistor::BasicResistor object.
|
14
14
|
# r1 = Resistor.new(ohm: 20)
|
15
15
|
# r2 = Resistor.new(code: ['yellow', 'purple', 'red', 'gold'])
|
16
|
-
def new(options = {})
|
17
|
-
Resistor::BasicResistor.new(options)
|
16
|
+
def new(arg, options = {})
|
17
|
+
Resistor::BasicResistor.new(arg, options)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -1,31 +1,33 @@
|
|
1
1
|
module Resistor
|
2
2
|
class BasicResistor
|
3
3
|
|
4
|
-
attr_reader :ohm, :code, :
|
4
|
+
attr_reader :ohm, :code, :tolerance
|
5
5
|
|
6
6
|
# Initializes a new BasicResistor object.
|
7
7
|
# Both the ohm and code parameter are optional,
|
8
8
|
# but at least one of them must be supplied.
|
9
9
|
#
|
10
|
-
# @
|
11
|
-
#
|
12
|
-
# @
|
10
|
+
# @overload initialize(arg, options = {})
|
11
|
+
# @param arg [Integer, Float] A resistance value.
|
12
|
+
# @overload initialize(arg, options = {})
|
13
|
+
# @param arg [Array<Symbol>, Array<String>] A color code.
|
14
|
+
# @option options [Integer, Float] :tolerance(5.0)
|
13
15
|
# @raise [ArgumentError] The ohm or code parameter must be supplied.
|
14
16
|
# Error raised if neither parameter is supplied.
|
15
17
|
# @return [Resistor::BasicResistor]
|
16
|
-
|
17
|
-
|
18
|
-
options[:
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
def initialize(arg, options = {})
|
19
|
+
options[:tolerance] ||= 5.0
|
20
|
+
opt = {:tolerance => options[:tolerance]}
|
21
|
+
|
22
|
+
case arg
|
23
|
+
when Integer, Float
|
24
|
+
@ohm = arg.to_f
|
22
25
|
@code = Resistor::ColorCode.encode(@ohm, opt)
|
23
|
-
@
|
24
|
-
|
25
|
-
|
26
|
-
@code = options[:code].map(&:to_sym)
|
26
|
+
@tolerance = options[:tolerance]
|
27
|
+
when Array
|
28
|
+
@code = arg.map(&:to_sym)
|
27
29
|
@ohm = Resistor::ColorCode.decode(@code)
|
28
|
-
@
|
30
|
+
@tolerance = Resistor::ColorCode::TOLERANCE[@code[3].to_sym]
|
29
31
|
else
|
30
32
|
raise ArgumentError
|
31
33
|
end
|
@@ -42,13 +44,13 @@ module Resistor
|
|
42
44
|
|
43
45
|
# Set a color code.
|
44
46
|
# When the color code is changed, the resistance value is also changed,
|
45
|
-
# and the
|
47
|
+
# and the tolerance is also changed.
|
46
48
|
#
|
47
49
|
# @param code [Array<Symbol>, Array<String>] color code
|
48
50
|
def code=(code)
|
49
51
|
@code = code.map(&:to_sym)
|
50
52
|
@ohm = Resistor::ColorCode.decode(@code)
|
51
|
-
@
|
53
|
+
@tolerance = Resistor::ColorCode::TOLERANCE[@code[3].to_sym]
|
52
54
|
end
|
53
55
|
|
54
56
|
# Calculates a series combined resistance value.
|
@@ -58,6 +60,7 @@ module Resistor
|
|
58
60
|
def +(other)
|
59
61
|
Resistor::CombinedResistor.new(@ohm + other.ohm)
|
60
62
|
end
|
63
|
+
alias_method :-, :+
|
61
64
|
|
62
65
|
# Calculates a parallel combined resistance value.
|
63
66
|
#
|
@@ -66,11 +69,12 @@ module Resistor
|
|
66
69
|
def /(other)
|
67
70
|
Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
|
68
71
|
end
|
72
|
+
alias_method :|, :/
|
69
73
|
|
70
74
|
# @return [Boolean] Whether or not the resistance value is the E12 series.
|
71
75
|
def e12?
|
72
|
-
num0 = Resistor::ColorCode::
|
73
|
-
num1 = Resistor::ColorCode::
|
76
|
+
num0 = Resistor::ColorCode::DIGIT[@code[0]]
|
77
|
+
num1 = Resistor::ColorCode::DIGIT[@code[1]]
|
74
78
|
Resistor::ColorCode::E12_SERIES.each do |key, val|
|
75
79
|
if num0 == key
|
76
80
|
return true if val.any? { |e| e == num1 }
|
@@ -81,8 +85,8 @@ module Resistor
|
|
81
85
|
|
82
86
|
# @return [Boolean] Whether or not the resistance value is the E24 series.
|
83
87
|
def e24?
|
84
|
-
num0 = Resistor::ColorCode::
|
85
|
-
num1 = Resistor::ColorCode::
|
88
|
+
num0 = Resistor::ColorCode::DIGIT[@code[0]]
|
89
|
+
num1 = Resistor::ColorCode::DIGIT[@code[1]]
|
86
90
|
Resistor::ColorCode::E24_SERIES.each do |key, val|
|
87
91
|
if num0 == key
|
88
92
|
return true if val.any? { |e| e == num1 }
|
data/lib/resistor/color_code.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
module Resistor
|
2
2
|
module ColorCode
|
3
3
|
|
4
|
-
|
5
|
-
NUM = {
|
4
|
+
DIGIT = {
|
6
5
|
:black => 0,
|
7
6
|
:brown => 1,
|
8
7
|
:red => 2,
|
@@ -15,8 +14,7 @@ module Resistor
|
|
15
14
|
:white => 9
|
16
15
|
}.freeze
|
17
16
|
|
18
|
-
|
19
|
-
MULT = {
|
17
|
+
MULTIPLIER = {
|
20
18
|
:black => 0,
|
21
19
|
:brown => 1,
|
22
20
|
:red => 2,
|
@@ -28,8 +26,7 @@ module Resistor
|
|
28
26
|
:silver => -2
|
29
27
|
}.freeze
|
30
28
|
|
31
|
-
|
32
|
-
ERROR_RANGE = {
|
29
|
+
TOLERANCE = {
|
33
30
|
:brown => 1.0,
|
34
31
|
:red => 2.0,
|
35
32
|
:orange => 0.05,
|
@@ -67,28 +64,28 @@ module Resistor
|
|
67
64
|
# The value must be between 0.1 and 99_000_000.
|
68
65
|
#
|
69
66
|
# @param ohm [Integer, Float] resistance value
|
70
|
-
# @option options [Integer, Float] :
|
67
|
+
# @option options [Integer, Float] :tolerance(5.0)
|
71
68
|
# @raise [ArgumentError] Error raised
|
72
69
|
# when the supplied resistance value is less than 0.1.
|
73
70
|
# @return [Array<Symbol>] color code
|
74
|
-
def self.encode(ohm, options = {:
|
75
|
-
return [
|
71
|
+
def self.encode(ohm, options = {:tolerance => 5.0})
|
72
|
+
return [DIGIT.key(0)] if ohm == 0
|
76
73
|
raise ArgumentError if ohm < 0.1
|
77
74
|
|
78
75
|
if ohm < 1
|
79
76
|
ohm_str = (ohm*100).to_s.split('')
|
80
|
-
[
|
81
|
-
|
77
|
+
[DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
|
78
|
+
MULTIPLIER.key(-2), TOLERANCE.key(options[:tolerance])]
|
82
79
|
|
83
80
|
elsif ohm < 10
|
84
81
|
ohm_str = (ohm*10).to_s.split('')
|
85
|
-
[
|
86
|
-
|
82
|
+
[DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
|
83
|
+
MULTIPLIER.key(-1), TOLERANCE.key(options[:tolerance])]
|
87
84
|
|
88
85
|
else
|
89
86
|
ohm_str = ohm.to_i.to_s.split('')
|
90
|
-
[
|
91
|
-
|
87
|
+
[DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
|
88
|
+
MULTIPLIER.key(ohm_str.size - 2), TOLERANCE.key(options[:tolerance])]
|
92
89
|
end
|
93
90
|
end
|
94
91
|
|
@@ -105,7 +102,7 @@ module Resistor
|
|
105
102
|
return 0.0 if code == [:black]
|
106
103
|
raise ArgumentError if code[0] == :black
|
107
104
|
|
108
|
-
ohm = (
|
105
|
+
ohm = (DIGIT[code[0]]*10 + DIGIT[code[1]]) * 10**MULTIPLIER[code[2]]
|
109
106
|
return ohm.to_f
|
110
107
|
end
|
111
108
|
end
|
@@ -20,6 +20,7 @@ module Resistor
|
|
20
20
|
def +(other)
|
21
21
|
Resistor::CombinedResistor.new(@ohm + other.ohm)
|
22
22
|
end
|
23
|
+
alias_method :-, :+
|
23
24
|
|
24
25
|
# Calculates a parallel combined resistance value.
|
25
26
|
#
|
@@ -28,5 +29,6 @@ module Resistor
|
|
28
29
|
def /(other)
|
29
30
|
Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
|
30
31
|
end
|
32
|
+
alias_method :|, :/
|
31
33
|
end
|
32
34
|
end
|
data/lib/resistor/version.rb
CHANGED
data/spec/basic_resistor_spec.rb
CHANGED
@@ -5,70 +5,83 @@ require_relative "spec_helper"
|
|
5
5
|
describe Resistor::BasicResistor do
|
6
6
|
|
7
7
|
describe '::new' do
|
8
|
-
let(:resistor) { Resistor.new(
|
8
|
+
let(:resistor) { Resistor.new(arg) }
|
9
9
|
|
10
10
|
context '抵抗値を指定した場合' do
|
11
|
-
let(:
|
12
|
-
let(:code) { nil }
|
11
|
+
let(:arg) { 4700 }
|
13
12
|
|
14
13
|
it { expect(resistor.ohm).to eq 4700.0 }
|
15
14
|
it { expect(resistor.code).to eq [:yellow, :purple, :red, :gold] }
|
16
|
-
it { expect(resistor.
|
15
|
+
it { expect(resistor.tolerance).to eq 5.0 }
|
17
16
|
end
|
18
17
|
|
19
18
|
context '抵抗値を指定した場合' do
|
20
|
-
let(:
|
21
|
-
let(:code) { ['brown', 'black', 'blue', 'silver'] }
|
19
|
+
let(:arg) { ['brown', 'black', 'blue', 'silver'] }
|
22
20
|
|
23
21
|
it { expect(resistor.ohm).to eq 10_000_000 }
|
24
22
|
it { expect(resistor.code).to eq [:brown, :black, :blue, :silver] }
|
25
|
-
it { expect(resistor.
|
23
|
+
it { expect(resistor.tolerance).to eq 10.0 }
|
26
24
|
end
|
27
25
|
|
28
26
|
context 'どちらも指定しなかった場合' do
|
29
|
-
let(:
|
30
|
-
let(:code) { nil }
|
27
|
+
let(:arg) { nil }
|
31
28
|
|
32
29
|
it { expect { is_expected }.to raise_error(ArgumentError) }
|
33
30
|
end
|
34
31
|
|
35
32
|
context '許容差を指定した場合' do
|
36
|
-
resistor = Resistor.new(
|
33
|
+
resistor = Resistor.new(100, tolerance: 0.1)
|
37
34
|
it { expect(resistor.code[3]).to eq :purple }
|
38
|
-
it { expect(resistor.
|
35
|
+
it { expect(resistor.tolerance).to eq 0.1 }
|
39
36
|
end
|
40
37
|
end
|
41
38
|
|
42
39
|
describe '#+, #/' do
|
43
40
|
context '直列接続の合成抵抗' do
|
44
|
-
r1 = Resistor.new(
|
45
|
-
r2 = Resistor.new(
|
46
|
-
r3 = Resistor.new(
|
41
|
+
r1 = Resistor.new(100)
|
42
|
+
r2 = Resistor.new(200)
|
43
|
+
r3 = Resistor.new(300)
|
47
44
|
|
48
45
|
it { expect((r1 + r2 + r3).ohm).to eq 600.0 }
|
49
46
|
end
|
50
47
|
|
51
48
|
context '並列接続の合成抵抗' do
|
52
|
-
r1 = Resistor.new(
|
53
|
-
r2 = Resistor.new(
|
54
|
-
r3 = Resistor.new(
|
49
|
+
r1 = Resistor.new(30)
|
50
|
+
r2 = Resistor.new(15)
|
51
|
+
r3 = Resistor.new(10)
|
55
52
|
|
56
53
|
it { expect((r1 / r2 / r3).ohm).to eq 5.0 }
|
57
54
|
end
|
58
55
|
|
59
56
|
context '並列と直列を合わせた合成抵抗' do
|
60
|
-
r1 = Resistor.new(
|
61
|
-
r2 = Resistor.new(
|
62
|
-
r3 = Resistor.new(
|
63
|
-
r4 = Resistor.new(
|
57
|
+
r1 = Resistor.new(20)
|
58
|
+
r2 = Resistor.new(30)
|
59
|
+
r3 = Resistor.new(4)
|
60
|
+
r4 = Resistor.new(8)
|
64
61
|
|
65
62
|
it { expect(((r1 / r2) + r3 + (r4 / r4)).ohm).to eq 20.0 }
|
66
63
|
end
|
64
|
+
|
65
|
+
context '#+のエイリアスメソッドを使用した場合' do
|
66
|
+
r1 = Resistor.new(100)
|
67
|
+
original_name = (r1 + r1 + r1).ohm
|
68
|
+
new_name = (r1 - r1 - r1).ohm
|
69
|
+
|
70
|
+
it { expect(original_name == new_name).to be_truthy }
|
71
|
+
end
|
72
|
+
|
73
|
+
context '#/のエイリアスメソッドを使用した場合' do
|
74
|
+
r1 = Resistor.new(100)
|
75
|
+
original_name = (r1 / r1 / r1).ohm
|
76
|
+
new_name = (r1 | r1 | r1).ohm
|
77
|
+
|
78
|
+
it { expect(original_name == new_name).to be_truthy }
|
79
|
+
end
|
67
80
|
end
|
68
81
|
|
69
82
|
describe '#ohm=, #code=' do
|
70
83
|
context '抵抗値を変更する場合' do
|
71
|
-
resistor = Resistor.new(
|
84
|
+
resistor = Resistor.new(100)
|
72
85
|
resistor.ohm = 4.7
|
73
86
|
|
74
87
|
it { expect(resistor.ohm).to eq 4.7 }
|
@@ -76,44 +89,44 @@ describe Resistor::BasicResistor do
|
|
76
89
|
end
|
77
90
|
|
78
91
|
context 'カラーコードを変更する場合' do
|
79
|
-
resistor = Resistor.new(
|
92
|
+
resistor = Resistor.new(100)
|
80
93
|
resistor.code = ['green', 'brown', 'silver', 'brown']
|
81
94
|
|
82
95
|
it { expect(resistor.ohm).to eq 0.51 }
|
83
96
|
it { expect(resistor.code).to eq [:green, :brown, :silver, :brown] }
|
84
|
-
it { expect(resistor.
|
97
|
+
it { expect(resistor.tolerance).to eq 1.0 }
|
85
98
|
end
|
86
99
|
end
|
87
100
|
|
88
101
|
describe '#e12?' do
|
89
102
|
context 'E12系列である場合' do
|
90
|
-
it { expect(Resistor.new(
|
91
|
-
it { expect(Resistor.new(
|
92
|
-
it { expect(Resistor.new(
|
93
|
-
it { expect(Resistor.new(
|
103
|
+
it { expect(Resistor.new(0.12).e12?).to eq true }
|
104
|
+
it { expect(Resistor.new(3.3).e12?).to eq true }
|
105
|
+
it { expect(Resistor.new(47).e12?).to eq true }
|
106
|
+
it { expect(Resistor.new(82_000_000).e12?).to eq true }
|
94
107
|
end
|
95
108
|
|
96
109
|
context 'E12系列でない場合' do
|
97
|
-
it { expect(Resistor.new(
|
98
|
-
it { expect(Resistor.new(
|
99
|
-
it { expect(Resistor.new(
|
100
|
-
it { expect(Resistor.new(
|
110
|
+
it { expect(Resistor.new(0.13).e12?).to eq false }
|
111
|
+
it { expect(Resistor.new(3.8).e12?).to eq false }
|
112
|
+
it { expect(Resistor.new(55).e12?).to eq false }
|
113
|
+
it { expect(Resistor.new(70_000_000).e12?).to eq false }
|
101
114
|
end
|
102
115
|
end
|
103
116
|
|
104
117
|
describe '#e24?' do
|
105
118
|
context 'E24系列である場合' do
|
106
|
-
it { expect(Resistor.new(
|
107
|
-
it { expect(Resistor.new(
|
108
|
-
it { expect(Resistor.new(
|
109
|
-
it { expect(Resistor.new(
|
119
|
+
it { expect(Resistor.new(0.1).e24?).to eq true }
|
120
|
+
it { expect(Resistor.new(3).e24?).to eq true }
|
121
|
+
it { expect(Resistor.new(47_000).e24?).to eq true }
|
122
|
+
it { expect(Resistor.new(10_000_000).e24?).to eq true }
|
110
123
|
end
|
111
124
|
|
112
125
|
context 'E24系列でない場合' do
|
113
|
-
it { expect(Resistor.new(
|
114
|
-
it { expect(Resistor.new(
|
115
|
-
it { expect(Resistor.new(
|
116
|
-
it { expect(Resistor.new(
|
126
|
+
it { expect(Resistor.new(0.92).e24?).to eq false }
|
127
|
+
it { expect(Resistor.new(8.3).e24?).to eq false }
|
128
|
+
it { expect(Resistor.new(23_000).e24?).to eq false }
|
129
|
+
it { expect(Resistor.new(52_000_000).e24?).to eq false }
|
117
130
|
end
|
118
131
|
end
|
119
132
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resistor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- seinosuke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- ".gitignore"
|
35
35
|
- ".rspec"
|
36
36
|
- Gemfile
|
37
|
+
- Guardfile
|
37
38
|
- LICENSE.txt
|
38
39
|
- README.md
|
39
40
|
- Rakefile
|
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
67
|
version: '0'
|
67
68
|
requirements: []
|
68
69
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
70
|
+
rubygems_version: 2.2.2
|
70
71
|
signing_key:
|
71
72
|
specification_version: 4
|
72
73
|
summary: Resistor gem
|