the dragon has-a traits object

Discussion in 'Ruby' started by Thufir, Nov 13, 2007.

  1. Thufir

    Thufir Guest

    Is this the "correct" way to change the dragon's traits?


    C:\code>
    C:\code>
    C:\code>composition.rb
    #<Dragon:0x277057c>

    C:\code>
    C:\code>
    C:\code>dir
    Volume in drive C has no label.
    Volume Serial Number is 0491-510F

    Directory of C:\code

    11/13/2007 01:36 PM <DIR> .
    11/13/2007 01:36 PM <DIR> ..
    11/13/2007 01:48 PM 81 composition.rb
    11/13/2007 01:49 PM 156 Creature.rb
    11/13/2007 01:46 PM 117 Dragon.rb
    11/13/2007 01:47 PM 246 Traits.rb
    4 File(s) 600 bytes
    2 Dir(s) 30,947,139,584 bytes free

    C:\code>
    C:\code>type composition.rb
    require 'Dragon'


    drgn = Dragon.new

    drgn.traits.life = 1340


    puts drgn
    C:\code>
    C:\code>type Dragon.rb
    require 'Creature'

    class Dragon < Creature

    def initialize ()
    super
    end

    def inspect ()
    super
    end

    end
    C:\code>
    C:\code>type Creature.rb
    require 'Traits'

    class Creature

    attr_accessor :traits


    def initialize ()
    @traits=Traits.new
    end

    def inspect ()
    @traits.inspect
    end

    end
    C:\code>
    C:\code>type Traits.rb
    class Traits

    attr_accessor :life, :strength, :charisma, :weapon

    def initialize ()
    @life = 0
    @strength = 0
    @charisma = 0
    @weapon = 0
    end

    def inspect ()
    puts @life
    puts @strength
    puts @charisma
    puts @weapon
    end


    end
    C:\code>
    C:\code>



    thanks,

    Thufir
    Thufir, Nov 13, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manu
    Replies:
    3
    Views:
    307
  2. ray
    Replies:
    1
    Views:
    1,302
    Robert Kern
    Jun 4, 2010
  3. Thufir
    Replies:
    3
    Views:
    137
    ara.t.howard
    Nov 8, 2007
  4. Thufir
    Replies:
    4
    Views:
    108
    7stud --
    Nov 8, 2007
  5. Thufir
    Replies:
    2
    Views:
    91
    Thufir
    Nov 13, 2007
Loading...

Share This Page