F. Senault
3/3/2008 6:02:00 PM
Le 03 mars à 17:53, August0866 a écrit :
> My intent is when i call class Npc(str) i want to get back
> 3 random values power,speed,smarts 2 derived values => accuracy and =>
> health and a derived string => job
>
> like this
> joe=Npc.new('Joe')
> puts joe
>
> output like "Joe" 11 12 15 10.15 8.8 "smithy"
Ok. Here's an adapted version of your code.
- I gave you the way to create a "Roll" objetct, but you don't really
need it ; what you need is an "helper function" that returns a
number. So, I made a module to contain it.
- Constructors in Ruby _always_ return the new object, regardless of the
explicit return clause.
- Rolling dice every time you access one of your base characteristics is
not the way to go ; here, I assign them at constructing time, and
give them an accessor.
- Using the #{} interpolation mechanism is way more legible than the
concatenation with xxx.to_s (BTW, inspect returns a string, no need
for to_s).
- Puts returns nil, not the string passed ; puts test where your test
method itself is writing on the screen is useless.
- If you want to convert your object to a string (for instance for
puts), all you need to do is define a to_s method.
Oh, and last : you should _really_ pay attention to the formatting of
your code...
class Roll
def Roll.roll(base, range)
base+rand(range)
end
end
class Stats
attr_reader :power, :speed, :smarts
def initialize()
@power = Roll.roll(10,8)
@speed = Roll.roll(10,8)
@smarts = Roll.roll(10,8)
end
end
class Attribs < Stats
def acc
(@power.to_f + (@speed.to_f / @smarts.to_f)).round
end
def health
(@power.to_f * (@smarts.to_f / @speed.to_f)).round
end
end
class Npc < Attribs
def initialize(name)
@name = name
super()
end
def job
'stuff'
end
def to_s
"#{@name} - pw#{power}, sp#{speed}, sm#{smarts}, ac#{acc}, " "he#{health} - #{job}"
end
end
class Test
def test
a = Roll.roll(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}"
puts "b methods are power = #{b.power}, "
puts "speed = #{b.speed}, "
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}"
puts " c has a speed of #{c.speed}"
puts " c has a smarts of #{c.smarts}"
puts " plus c has an Accuracy of #{c.acc}"
puts " plus c has a health of #{c.health}"
puts "________________C____________________"
puts "||||||||||||||||D||||||||||||||||||||"
puts 'D is the goal'
puts d.inspect
puts d
end
end
s=Test.new
s.test
Fred
--
Yes, Java is so bulletproofed that to a C programmer it feels like
being in a straightjacket, but it's a really comfy and warm
straightjacket, and the world would be a safer place if everyone was
straightjacketed most of the time. (Mark Hughes in the SDM)