Class: Resistor::BasicResistor

Inherits:
Object
  • Object
show all
Defined in:
lib/resistor/basic_resistor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Overloads:

  • #initialize(arg, options = {}) ⇒ Resistor::BasicResistor

    Parameters:

    • arg (Integer, Float)

      A resistance value

  • #initialize(arg, options = {}) ⇒ Resistor::BasicResistor

    Parameters:

    • arg (Array<Symbol>, Array<String>)

      A color code

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :tolerance(5.0) (Integer, Float)
  • :band_number(4) (Integer)

Raises:

  • (ArgumentError)

    The ohm or code parameter must be supplied. Error raised if neither parameter is 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, options = {})
  default = Resistor::Options.new
  options[:tolerance] ||= default.tolerance
  options[:band_number] ||= default.band_number

  case arg
  when Integer, Float
    @ohm = arg.to_f
    @code = Resistor::ColorCode.encode(@ohm, options)
    @tolerance = options[:tolerance]
  when Array
    @code = arg.map(&:to_sym)
    @ohm = Resistor::ColorCode.decode(@code, options)
    @tolerance = Resistor::ColorCode::TOLERANCE[@code[-1].to_sym]
  else
    raise ArgumentError
  end
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



4
5
6
# File 'lib/resistor/basic_resistor.rb', line 4

def code
  @code
end

#ohmObject

Returns the value of attribute ohm.



4
5
6
# File 'lib/resistor/basic_resistor.rb', line 4

def ohm
  @ohm
end

#toleranceObject (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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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