[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class and inheritance

August0866

3/3/2008 3:04:00 PM

Hi All,

I am not a programmer, yet. I am trying ruby on for size, most things
fit ok, but Classes are puzzling me.

<code>
class Roll
def Roll(base,range)
@base=base
@range=range
@roll = base+random(range)
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=0.75*self.power+(self.speed/self.smarts)
end
def health
@health = self.power*(self.smarts/self.speed)
end
end
class Npc < Attribs
def name(name)
@name=name
end
end

joe=Npc.new
joe.name('Joe')
#puts joe.name.to_s+' = name'
puts joe.power.to_s+' = power'
puts joe.smarts.to_s+' = smarts'
puts joe.speed.to_s+' = speed'
puts joe.health.to_s+' = health'
puts joe.acc+' = acc'
puts joe
puts '======================='
puts 'Calculations'
puts 0.75*joe.power+(joe.speed/joe.smarts)
</code>

OK, so why is this broken?
19 Answers

F. Senault

3/3/2008 3:15:00 PM

0

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.
>
> <code>
> 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
--
- The light at the end of the tunnel is the explosives around that
little ball of Pu239. (Mike Andrews in the SDM)
- The darkness at the end of the tunnel is an oncoming train with
broken headlights. (Lars Balker Rasmussen in the SDM)

August0866

3/3/2008 4:37:00 PM

0

On Mar 3, 10:15 am, "F. Senault" <f...@lacave.net> 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.
>
> > <code>
> > 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

August0866

3/3/2008 4:53:00 PM

0

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"

F. Senault

3/3/2008 6:02:00 PM

0

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)

August0866

3/3/2008 7:40:00 PM

0

On Mar 3, 1:01 pm, "F. Senault" <f...@lacave.net> wrote:
> Le 03 mars à 17:53, August0866 a écrit :

Thank you for taking time i appreciate the help,

>
> Oh, and last : you should _really_ pay attention to the formatting of
> your code...
>

being painfully new at this i don't know what you mean, what can i do
better format wise next time

F. Senault

3/3/2008 9:14:00 PM

0

Le 03 mars à 20:39, August0866 a écrit :

> On Mar 3, 1:01 pm, "F. Senault" <f...@lacave.net> wrote:

>> Oh, and last : you should _really_ pay attention to the formatting of
>> your code...
>>
>
> being painfully new at this i don't know what you mean, what can i do
> better format wise next time

First and before all, indentation. Try to indent your statements by two
spaces, and to keep the code aligned (the end keyword should be at the
same column than the corresponding def or class keyword, etc).

After that, try to add some space around operators and such.

It makes code much more legible, which is important when you write code
that you'll have to understand later, and doubly so if you post it to a
public forum where _other_ people have to understand it...

CYA,

Fred
--
- I've always maintained that no software should be released that can't
withstand three hours of enthusiastic yet undirected pounding-on by a
typical five-year-old. - You've just described Microsoft's entire
development process. (Joe Thompson & Malcolm Ray in the SDM)

Arlen Cuss

3/4/2008 5:53:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Tue, Mar 4, 2008 at 5:04 AM, F. Senault <fred@lacave.net> wrote:

> - 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.
>

class Roll
> def Roll.roll(base, range)
> base+rand(range)
> end
> end
>

You could use a regular ol'module for that:

module Roll
def self.roll(base, range)
base + rand(range)
end
end

Arlen

F. Senault

3/4/2008 2:28:00 PM

0

Le 04 mars à 06:52, Arlen Cuss a écrit :

> You could use a regular ol'module for that:
>
> module Roll
> def self.roll(base, range)
> base + rand(range)
> end
> end

Indeed. I wanted to make that modification, but it seems I forgot it...
Thanks.

Fred
--
All that is now All that is gone All that's to come
And everything under the sun is in tune
But the sun is eclipsed by the moon
(Pink Floyd, Eclipse)

August0866

3/4/2008 2:47:00 PM

0

OK, 2 dumb questions What is the difference between a class and a
module and

What is wrong here, the logic or the syntax

<stub location =( class => Npc, def => job)>

def job
if
@power.to_f > 12 && @smarts.to_f > 14 && @speed.to_f > 14
@job = 'Adventurer'
elsif
@power.to_f > 14 and (@smarts.to_f <14 or @speed.to_f < 14)
@job = 'laborer'
elsif
@power.to_f < 14 and (@smarts.to_f > 14 and @speed.to_f > 14)
@job = 'coureror'
elsif
@power.to_f < 14 and (@smarts.to_f > 14 and @speed.to_f < 14)
@job='teacher'
else
@job = 'commoner'
end
end
</stub>

the result is sometimes joe is an adventurer otherwise nothing is
output

Gary Wright

3/4/2008 4:54:00 PM

0


On Mar 4, 2008, at 9:50 AM, August0866 wrote:

> OK, 2 dumb questions What is the difference between a class and a
> module and

A module is a container for methods and a namespace
for constants.

A class is a module (Class inherits from Module) that
provides a framework for instantiating and initializing
instances as well as organizing method lookup through
the inheritance hierarchy.

If you think of classes as blueprints for creating
instances, then modules are like common sub-systems
that are reused in the blueprints of one or more
classes.

Gary Wright