I’m a newbie to Ruby (hmm… rhymes) and to object oriented programming
in general, so having a hard time wrapping my mind around a paradigm
which differs from C language. Here is a short snippet of code, the
general flow coming from the book “The Ruby Way” (pg 392).
The class ExternalBallistics would be the equivalent of my top level
function (or container of functions) which will get inputs from the Main
(in this case rails text and selection box fields) and will calculate a
series of results.
desktop is supposed to define those variables, which ExternalBallistics
can then apply to its various methods. However, it is not working out
that way. Ruby is complaining that the variables a, b and c are not
defined. Note that the relationship between ExternalBallistics and
BasicMath is OK, but the relationship between desktop and
ExternalBallistics is not, as if I am not using the attributes
incorrectly. Can someone help me here.
class BasicMath
Could be called as a class, static, method
attr_accessor :a, :b, :c
def add(a, b, c)
m = (a + b - c)/b
n = a + b
return m, n
end
def calc_nrows(a, b, c)
nrows = (a - b)/c
return nrows.round
end
end
class ExternalBallistics
attr_accessor :a,:b,:c
def initialize(&block)
instance eval &block
end
tc = BasicMath.new
puts tc.add(a,b,c)
puts tc.calc_nrows(5.0,3.0,1.0)
end
desktop = ExternalBallistics.new do
self.a = 5.0
self.b = 3.0
self.c = 1.0
end