Class: Resistor::BasicResistor
- Inherits:
-
Object
- Object
- Resistor::BasicResistor
- Defined in:
- lib/resistor/basic_resistor.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#ohm ⇒ Object
Returns the value of attribute ohm.
-
#tolerance ⇒ Object
readonly
Returns the value of attribute tolerance.
Instance Method Summary collapse
-
#+(other) ⇒ Resistor::CombinedResistor
(also: #-)
Calculates a series combined resistance value.
-
#/(other) ⇒ Resistor::CombinedResistor
(also: #|)
Calculates a parallel combined resistance value.
-
#e12? ⇒ Boolean
Whether or not the resistance value is the E12 series.
-
#e24? ⇒ Boolean
Whether or not the resistance value is the E24 series.
-
#e48? ⇒ Boolean
Whether or not the resistance value is the E48 series.
-
#e96? ⇒ Boolean
Whether or not the resistance value is the E96 series.
-
#initialize(arg, options = {}) ⇒ Resistor::BasicResistor
constructor
Initializes a new BasicResistor object.
Constructor Details
#initialize(arg, options = {}) ⇒ Resistor::BasicResistor #initialize(arg, options = {}) ⇒ Resistor::BasicResistor
Initializes a new BasicResistor object. Both the ohm and code parameter are optional, but at least one of them must be supplied.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/resistor/basic_resistor.rb', line 19 def initialize(arg, = {}) default = Resistor::Options.new [:tolerance] ||= default.tolerance [:band_number] ||= default.band_number case arg when Integer, Float @ohm = arg.to_f @code = Resistor::ColorCode.encode(@ohm, ) @tolerance = [:tolerance] when Array @code = arg.map(&:to_sym) @ohm = Resistor::ColorCode.decode(@code, ) @tolerance = Resistor::ColorCode::TOLERANCE[@code[-1].to_sym] else raise ArgumentError end end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
4 5 6 |
# File 'lib/resistor/basic_resistor.rb', line 4 def code @code end |
#ohm ⇒ Object
Returns the value of attribute ohm.
4 5 6 |
# File 'lib/resistor/basic_resistor.rb', line 4 def ohm @ohm end |
#tolerance ⇒ Object (readonly)
Returns the value of attribute tolerance.
4 5 6 |
# File 'lib/resistor/basic_resistor.rb', line 4 def tolerance @tolerance end |
Instance Method Details
#+(other) ⇒ Resistor::CombinedResistor Also known as: -
Calculates a series combined resistance value.
62 63 64 |
# File 'lib/resistor/basic_resistor.rb', line 62 def +(other) Resistor::CombinedResistor.new(@ohm + other.ohm) end |
#/(other) ⇒ Resistor::CombinedResistor Also known as: |
Calculates a parallel combined resistance value.
71 72 73 |
# File 'lib/resistor/basic_resistor.rb', line 71 def /(other) Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm)) end |
#e12? ⇒ Boolean
Whether or not the resistance value is the E12 series.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/resistor/basic_resistor.rb', line 79 def e12? num0 = Resistor::ColorCode::DIGIT[@code[0]] num1 = Resistor::ColorCode::DIGIT[@code[1]] Resistor::ColorCode::E12_SERIES.each do |key, val| if num0 == key return true if val.any? { |e| e == num1 } end end return false end |
#e24? ⇒ Boolean
Whether or not the resistance value is the E24 series.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/resistor/basic_resistor.rb', line 93 def e24? num0 = Resistor::ColorCode::DIGIT[@code[0]] num1 = Resistor::ColorCode::DIGIT[@code[1]] Resistor::ColorCode::E24_SERIES.each do |key, val| if num0 == key return true if val.any? { |e| e == num1 } end end return false end |
#e48? ⇒ Boolean
Whether or not the resistance value is the E48 series. Always returns false if the number of bands is 4.
108 109 110 111 112 113 114 115 116 |
# File 'lib/resistor/basic_resistor.rb', line 108 def e48? return false if @code.size == 4 num = [ Resistor::ColorCode::DIGIT[@code[0]], Resistor::ColorCode::DIGIT[@code[1]], Resistor::ColorCode::DIGIT[@code[2]], ].join.to_i Resistor::ColorCode::E48_SERIES.any?{|n| n == num } end |
#e96? ⇒ Boolean
Whether or not the resistance value is the E96 series. Always returns false if the number of bands is 4.
122 123 124 125 126 127 128 129 130 |
# File 'lib/resistor/basic_resistor.rb', line 122 def e96? return false if @code.size == 4 num = [ Resistor::ColorCode::DIGIT[@code[0]], Resistor::ColorCode::DIGIT[@code[1]], Resistor::ColorCode::DIGIT[@code[2]], ].join.to_i Resistor::ColorCode::E96_SERIES.any?{|n| n == num } end |