J
Jim Carter
I have the following configuration:
class ExternalBallistics
attr_accessor :a,:b #inputs
attr_reader :c #outputs
def initialize(&block)
instance_eval &block
end
def calculate
c = a + b
end
end #class
ExternalBallistics.new do
self.a = 5
self.b = 3
calculate
puts self.c
end #ExternalBallistics
This returns a nil value for c. If I use "puts" inside of def
calculate, I will get 8 for an answer. Why does a and b act globally
and c not. Am I forced to use a return on calculate to get an answer.
If I do so, do I still need to define an attr_reader :c (what use is it
then) ?
Thanks
class ExternalBallistics
attr_accessor :a,:b #inputs
attr_reader :c #outputs
def initialize(&block)
instance_eval &block
end
def calculate
c = a + b
end
end #class
ExternalBallistics.new do
self.a = 5
self.b = 3
calculate
puts self.c
end #ExternalBallistics
This returns a nil value for c. If I use "puts" inside of def
calculate, I will get 8 for an answer. Why does a and b act globally
and c not. Am I forced to use a return on calculate to get an answer.
If I do so, do I still need to define an attr_reader :c (what use is it
then) ?
Thanks