Professional Web Applications Themes

inheriting constants - Ruby

is this an appropriate way to inherit constants? ~/eg/ruby > cat a.rb #!/usr/bin/env ruby class A OPTIONS = 1, 0 def klass; self.class; end def options opts = [] k = klass loop do opts.push(*k::OPTIONS) k = k.superclass break unless defined?(k::OPTIONS) end opts end end class B < A OPTIONS = 3, 2 end class C < B OPTIONS = 5, 4 end p A.new.options p B.new.options p C.new.options ~/eg/ruby > ruby a.rb [1, 0] [3, 2, 1, 0] [5, 4, 3, 2, 1, 0] -a -- ================================================== ============================= | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] ...

  1. #1

    Default inheriting constants


    is this an appropriate way to inherit constants?

    ~/eg/ruby > cat a.rb
    #!/usr/bin/env ruby

    class A
    OPTIONS = 1, 0

    def klass; self.class; end

    def options
    opts = []

    k = klass
    loop do
    opts.push(*k::OPTIONS)
    k = k.superclass
    break unless defined?(k::OPTIONS)
    end

    opts
    end
    end

    class B < A
    OPTIONS = 3, 2
    end

    class C < B
    OPTIONS = 5, 4
    end

    p A.new.options
    p B.new.options
    p C.new.options

    ~/eg/ruby > ruby a.rb
    [1, 0]
    [3, 2, 1, 0]
    [5, 4, 3, 2, 1, 0]



    -a
    --
    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ================================================== =============================

    Ara.T.Howard Guest

  2. #2

    Default Re: inheriting constants


    "Ara.T.Howard" <gov> schrieb im Newsbeitrag
    news:ngdc.noaa.gov... 

    Looks ok to me. It's just that I don't like unnecessary "breaks" of loops
    which is why I'd define options() like this:

    def options
    opts = []
    k = self.class

    while k && defined?(k::OPTIONS)
    opts.concat(k::OPTIONS)
    k = k.superclass
    end

    opts
    end

    :-)

    robert


    Robert Guest

  3. #3

    Default Re: inheriting constants

    Ara.T.Howard wrote: 

    You are organizing your options as a set, but if you are interested in a
    hash model, you might be interested in a "class_superhash", which is
    sort of a hash that inherits key-value pairs according to the class
    hierarchy. F'rexample:

    require 'superhash'

    class A
    class_superhash :options

    options[:foo] = 1

    def options; self.class.options; end
    end

    class B < A
    options[:bar] = 2
    end

    p A.options
    p B.options.to_hash
    p B.new.options.to_hash

    # Output:
    # {:foo=>1}
    # {:foo=>1, :bar=>2}
    # {:foo=>1, :bar=>2}

    The underlying superhash objects support the same interface as a hash.
    (This is in my superhash package on RAA.)


    Joel Guest

Similar Threads

  1. Inheriting a control
    By Ant in forum ASP.NET Building Controls
    Replies: 1
    Last Post: July 24th, 04:10 AM
  2. inheriting Datagrid
    By King Kong in forum ASP.NET Building Controls
    Replies: 1
    Last Post: March 30th, 01:53 PM
  3. Inheriting style and properties
    By Jay in forum ASP.NET Building Controls
    Replies: 3
    Last Post: July 27th, 06:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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