Class: Eletro::Source
Instance Attribute Summary collapse
-
#circuits ⇒ Object
Returns the value of attribute circuits.
-
#hz ⇒ Object
Returns the value of attribute hz.
-
#v ⇒ Object
Returns the value of attribute v.
Attributes inherited from Part
Instance Method Summary collapse
- #<<(part, circuit = nil) ⇒ Object
- #ac? ⇒ Boolean
-
#acdc ⇒ Object
just for fun.
- #calc_sum_net(ary = circuits) ⇒ Object
- #dc? ⇒ Boolean
- #i ⇒ Object
-
#initialize(*args) ⇒ Source
constructor
A new instance of Source.
- #parallel_sum(r1, r2, *rest) ⇒ Object
- #serial_sum(ary) ⇒ Object
-
#sum_net ⇒ Object
Logic:.
- #w ⇒ Object
Methods inherited from Part
#*, #+, #-, #/, #abs, #coerce, #method_missing, #round, #to_f, #to_i, #to_s
Constructor Details
#initialize(*args) ⇒ Source
Returns a new instance of Source.
8 9 10 11 12 13 |
# File 'lib/eletro/source.rb', line 8 def initialize(*args) params = args.join @v = params.match(/(\d*)/)[1].to_i @hz = params =~ /ac|AC/ ? 60 : 0 @circuits = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Eletro::Part
Instance Attribute Details
#circuits ⇒ Object
Returns the value of attribute circuits.
5 6 7 |
# File 'lib/eletro/source.rb', line 5 def circuits @circuits end |
#hz ⇒ Object
Returns the value of attribute hz.
5 6 7 |
# File 'lib/eletro/source.rb', line 5 def hz @hz end |
#v ⇒ Object
Returns the value of attribute v.
5 6 7 |
# File 'lib/eletro/source.rb', line 5 def v @v end |
Instance Method Details
#<<(part, circuit = nil) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/eletro/source.rb', line 17 def <<(part, circuit=nil) if circuit else circuits << (part.is_a?(Array) ? part : [part]) end end |
#ac? ⇒ Boolean
80 81 82 |
# File 'lib/eletro/source.rb', line 80 def ac? !dc? end |
#acdc ⇒ Object
just for fun
72 73 74 |
# File 'lib/eletro/source.rb', line 72 def acdc dc? || (@v /= 2) end |
#calc_sum_net(ary = circuits) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/eletro/source.rb', line 51 def calc_sum_net(ary=circuits) ary.map! do |i| if i.is_a?(Array) i = [calc_sum_net(i)] unless i.select { |c| c.is_a?(Array) }.empty? i.size > 1 ? serial_sum(i) : i[0]#.value else; i end end ary.size == 1 ? ary[0] : parallel_sum(*ary) end |
#dc? ⇒ Boolean
76 77 78 |
# File 'lib/eletro/source.rb', line 76 def dc? @hz.zero? end |
#i ⇒ Object
25 26 27 |
# File 'lib/eletro/source.rb', line 25 def i @v / sum_net.to_f end |
#parallel_sum(r1, r2, *rest) ⇒ Object
62 63 64 65 |
# File 'lib/eletro/source.rb', line 62 def parallel_sum(r1, r2, *rest) sum = r1 * r2 / (r1 + r2) rest.empty? ? sum : parallel_sum(*rest.unshift(sum)) end |
#serial_sum(ary) ⇒ Object
67 68 69 |
# File 'lib/eletro/source.rb', line 67 def serial_sum(ary) ary.reduce(0) { |i, c| i + c } end |
#sum_net ⇒ Object
Logic:
Resistor on an array are in series, sum. [100, 100] => [200] Arrays in arrays are parallel connections. [[100], [100]] => [50]
Mixed example (rounded):
- [100], [100, 100], [100, 100]
- [100], [200], [200]
- [66], [200]
- [50]
- [100], [200], [200]
-
> 50 ohms
46 47 48 49 |
# File 'lib/eletro/source.rb', line 46 def sum_net # Memoize @sum_net ||= calc_sum_net end |
#w ⇒ Object
29 30 31 |
# File 'lib/eletro/source.rb', line 29 def w @v * i end |