From: August0866 Date: 2008-03-04T01:39:54+09:00 Subject: Re: Class and inheritance On Mar 3, 10:15 am, "F. Senault" wrote: > Le 03 mars à 16:03, August0866 a écrit : > > Hi. > > > I am not a programmer, yet. I am trying ruby on for size, most things > > fit ok, but Classes are puzzling me. > > > > > class Roll > > def Roll(base,range) > > @power=Roll.new(10,8) > > OK, so why is this broken? > > The constructor in ruby is named initialize, not the name of the class > like in Java. Try : > > class Roll > def initialize(base, range) > ... > end > ... > end > > Fred ok, new attempt class Roll def initialize(base, range) @roll = base+rand(range) return @roll end end class Stats def power @power=Roll.new(10,8) end def speed @speed=Roll.new(10,8) end def smarts @smarts=Roll.new(10,8) end end class Attribs < Stats def acc @acc=self.power+(self.speed / self.smarts)# doesnt want to do the math ?Why? end def health @health = 0.00+self.power*(self.smarts/self.speed)# doesnt want to do the math ?Why? end end class Npc < Attribs def initialize(name) @name=name end def job puts 'stuff' end end class Test def test a=Roll.new(10,8) b=Stats.new c=Attribs.new d=Npc.new('Joe') puts '++++++++++++++++A+++++++++++++++++++' puts 'a object is '+a.to_s puts 'a\'s method is called roll '+a.inspect puts '++++++++++++++++A+++++++++++++++++++' puts '================B===================' puts 'b is object '+b.inspect.to_s puts 'b methods are power = '+b.power.to_s puts ', speed ='+b.speed.to_s puts ', and finaly smarts = '+b.smarts.to_s puts '================B==================' puts '________________C__________________' puts 'now on to c' puts c.to_s+' is the object name for c' puts 'c has all the methods of a and b plus its own' puts ' c has a power of '+c.power.inspect.to_s puts ' c has a speed of '+c.speed.inspect.to_s puts ' c has a smarts of '+c.smarts.inspect.to_s puts ' plus c has an Accuracy of '+c.acc.to_s puts ' plus c has a health of '+c.health.to_s puts '________________C____________________' puts '||||||||||||||||D||||||||||||||||||||' puts 'D is the goal' puts d.inspect end end s=Test.new puts s.test