From: Josh Cheek Date: 2011-01-26T04:06:24+09:00 Subject: Re: General Ruby Object Oriented --00163630f5f38d5af8049ab069a4 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Jan 25, 2011 at 12:33 PM, Jim Carter wrote: > > def initialize(&block) > instance eval &block > end > > This should be instance_eval (with an underscore) > tc = BasicMath.new > puts tc.add(a,b,c) > puts tc.calc_nrows(5.0,3.0,1.0) > > This code is inside your class definition. So, at that time, self is that class, not an instance of that class. How about wrapping it in a method called calculate, so that its instances can call that code by invoking the calculate method: def calculate 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 > Then we would just need to stick a call to calculate in the block: desktop = ExternalBallistics.new do self.a = 5.0 self.b = 3.0 self.c = 1.0 calculate end On Tue, Jan 25, 2011 at 12:54 PM, Jim Carter wrote: > > but I don't understand the authors assignment of self.a, self.b, self.c, > especially in light of the fact that it doesn't work. If anyone can > share any insight, I'd appreciate it. > > In ExternalBallistics#initialize, you instance_eval the block. That sets self to be the instance that is created when you do ExternalBallistics.new, so when you are in the block and say self.a=5.0, you are telling it to invoke the method a= and pass it the value 5.0 --00163630f5f38d5af8049ab069a4--