Class: Eletro::Ohm

Inherits:
Object
  • Object
show all
Defined in:
lib/eletro/ohm.rb

Constant Summary collapse

VALUES =
[:v, :r, :i]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Ohm

Returns a new instance of Ohm.



8
9
10
# File 'lib/eletro/ohm.rb', line 8

def initialize(*args)
  args.each { |a| puts a.inspect }
end

Instance Attribute Details

#iObject

Returns the value of attribute i.



5
6
7
# File 'lib/eletro/ohm.rb', line 5

def i
  @i
end

#pObject

Returns the value of attribute p.



5
6
7
# File 'lib/eletro/ohm.rb', line 5

def p
  @p
end

#rObject

Returns the value of attribute r.



5
6
7
# File 'lib/eletro/ohm.rb', line 5

def r
  @r
end

#vObject

def r

calc(:r) || @r = v / i

end



17
18
19
# File 'lib/eletro/ohm.rb', line 17

def v
  @v
end

Instance Method Details

#calc(v, data = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/eletro/ohm.rb', line 29

def calc(v, data=nil)
  if data
    # instance_variable_set("@#{v}", BigDecimal.new(data.to_s))
   # puts data.class
    instance_variable_set("@#{v}", data)
  else
#     val = instance_variable_get("@#{v}")
#     return val if val

  (VALUES - [v]).select { |v| instance_variable_get("@#{v}").nil? }.empty?

#       raise "Can't call dunno #{v}" unless instance_variable_get("@#{v}")
#     end
  end
end

#to_sObject



45
46
47
48
49
50
51
52
53
# File 'lib/eletro/ohm.rb', line 45

def to_s
  o = ""
  VALUES.each do |v|
    if val = send(v)
      o += "#{val} " + { :r => "Ω", :i => "A", :v => "V"}[v]
    end
  end
  o
end