Ruby OO-newbie design-question

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Ruby OO-newbie design-question

    Hi!
    I'm not at all new to programming, I know OO-concepts but I don't have any
    hands-on experience with designing/programming OO except for small tests.
    I'm also a Ruby newbie, but have been coding a LOT of C, (advanced)
    Shellscripts and Python.

    I need advice on good design, taking features of Ruby into consideration.

    I want a tree of objects like this:

    a1
    b1
    c1
    b2
    c1
    c2
    d1

    and so on... The a#-objects will hold default values and methods for b# and
    b# will hold default values for c# and so on. When a value in a# is changed
    I want its children to immediatly inherit them also, if they aren't
    overridden. b# will be subclasses of a#, c# will be subclasses of b# etc.
    I have to be able to dynamically (at runtime) add and delete children.

    What is a good way to design this with the OO-features of Ruby, keeping it
    as readable and clean as possible?

    Regards,
    Per Wigren

    Per Wigren Guest

  2. Similar Questions and Discussions

    1. newbie question for Ruby and/or PHP
      Hi, with some of your help, I manage to extract a delimited record into mysql database using ruby. I have the following structure in my MySQL...
    2. Newbie question : Extending Ruby
      I'm making my first extension to Ruby. The C file declarations look like: __declspec(dllexport) VALUE cImage8 = Qnil; __declspec(dllexport)...
    3. Ruby and OOP-design (question of an old "procedural person" ;)
      Hi ! I have a newbie question on OOP-design: What I have is a large ASCII-File, a list of about 11000 shortwave broadcasters. Each line...
    4. (German) Article about Design Patterns in Ruby online
      http://www.linuxenterprise.de/itr/online_artikel/psecom,id,407,nodeid,9.html at this URL you can find a very good and interesting article by...
    5. [ruby-talk] newbie locale/ascii sort question
      Hi, In message " Re: newbie locale/ascii sort question" on 03/07/01, Bengt Dahlqvist <bengt.dahlqvist@ling.uu.se> writes: |Does such utilities...
  3. #2

    Default Re: Ruby OO-newbie design-question

    Hi Per,

    Unless a, b, and c have different behavior (not just values), I think you
    can do it with a single class:

    class Node
    include Enumerable

    def initialize
    @parent = nil
    @children = []
    @values = {}
    end

    def each(&b)
    @children.each &b
    end

    def add_child(child)
    child.parent = self
    @children << child
    end

    def remove_child(child)
    child.parent = nil
    @children.delete(child)
    end

    def [](key)
    # if the value exists locally, return it; otherwise, if parent
    exists, ask it
    @values[key] or (@parent and @parent[key])
    end

    def []=(key, value)
    @values[key] = value
    end

    protected

    def parent=(parent)
    @parent = parent
    end
    end

    # Then you can do this:
    a1 = Node.new
    b1 = Node.new
    a1.add_child(b1)
    c1 = Node.new
    b1.add_child(c1)
    b2 = Node.new
    a1.add_child(b2)
    # and so on...

    a1['color'] = 'purple'
    b1['color'] = 'yellow'
    puts b2['color'] # 'purple'
    puts c1['color'] # 'yellow'
    puts c1['nothing'] # nil

    Is that what you had in mind?

    "Per Wigren" <wigren@home.se> wrote in message news:3f4f3875_2@127.0.0.1...
    > Hi!
    > I'm not at all new to programming, I know OO-concepts but I don't have any
    > hands-on experience with designing/programming OO except for small tests.
    > I'm also a Ruby newbie, but have been coding a LOT of C, (advanced)
    > Shellscripts and Python.
    >
    > I need advice on good design, taking features of Ruby into consideration.
    >
    > I want a tree of objects like this:
    >
    > a1
    > b1
    > c1
    > b2
    > c1
    > c2
    > d1
    >
    > and so on... The a#-objects will hold default values and methods for b#
    and
    > b# will hold default values for c# and so on. When a value in a# is
    changed
    > I want its children to immediatly inherit them also, if they aren't
    > overridden. b# will be subclasses of a#, c# will be subclasses of b# etc.
    > I have to be able to dynamically (at runtime) add and delete children.
    >
    > What is a good way to design this with the OO-features of Ruby, keeping it
    > as readable and clean as possible?
    >
    > Regards,
    > Per Wigren
    >

    Joe Cheng Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139