Creature module for the Dragon class

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

  1. Thufir

    Thufir Guest

    I could have "instance" variables in Creature, which Dragon objects
    can use? Dragon would have all the methods of the Creature module
    available, for instance attribute accessors?



    C:\code>
    C:\code>
    C:\code>type Creature.rb
    module Creature


    def sayHi
    print "\nhi\n\n"
    end


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


    class Dragon
    extend Creature
    include Creature


    def toString
    print "\ndragon\n"
    end


    end
    C:\code>


    thanks,

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

  2. Thufir

    7stud -- Guest

    Thufir wrote:
    > I could have "instance" variables in Creature, which Dragon objects
    > can use? Dragon would have all the methods of the Creature module
    > available, for instance attribute accessors?
    >



    module Creature
    def kingdom=(some_kingdom)
    @kingdom = some_kingdom
    end

    def kingdom
    @kingdom
    end
    end


    class Dragon
    include Creature
    end

    d = Dragon.new
    d.kingdom = 'Mordor'
    puts d.kingdom

    --output:--
    Mordor
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Nov 9, 2007
    #2
    1. Advertising

  3. Thufir

    Thufir Guest

    On Nov 8, 10:32 pm, 7stud -- <> wrote:
    [...]
    > module Creature
    > def kingdom=(some_kingdom)
    > @kingdom = some_kingdom
    > end
    >
    > def kingdom
    > @kingdom
    > end
    > end
    >
    > class Dragon
    > include Creature
    > end
    >
    > d = Dragon.new
    > d.kingdom = 'Mordor'
    > puts d.kingdom
    >
    > --output:--
    > Mordor


    This is interesting because it brings up the next problem:
    locations. In this case, kingdoms. I changed the design a bit so
    that Creature is a class from which Dragon and so forth inherit. One
    approach I was thinking about would be to give each creature instance
    a few objects, such as: statistics, kingdom and inventory. (Right
    now the "statistics" portion is built in directly to the class, I'm
    considering making a class for these traits.)

    Each Creature needs a kingdom (even if it's "banished" or something),
    and, of course, a kingdom can "hold" many creature objects. In
    tables, the "kingdom" table would have a one-to-many relation to the
    "creatures" table.

    Just thinking outloud....



    -Thufir
     
    Thufir, Nov 10, 2007
    #3
    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:
    312
  2. Thufir
    Replies:
    3
    Views:
    144
    ara.t.howard
    Nov 8, 2007
  3. Thufir
    Replies:
    4
    Views:
    116
    7stud --
    Nov 8, 2007
  4. Thufir
    Replies:
    2
    Views:
    95
    Thufir
    Nov 13, 2007
  5. thufir

    cannot load file Creature.rb

    thufir, Mar 23, 2012, in forum: Ruby
    Replies:
    1
    Views:
    723
    thufir
    Mar 23, 2012
Loading...

Share This Page