On Wed, 7 Sep 2005, Joel VanderWerf wrote:

> IIUC, this situation calls for constants with dynamic scoping, e.g.
> mod::CONST.
>
>    class Element < Object; end
>
>    class TextField < Element
>        SUPPORTED_TYPES = ["text", "password", "textarea"]
>        def supported_types
>          self.class::SUPPORTED_TYPES
>        end
>    end
>
>    class Hidden < TextField
>        SUPPORTED_TYPES = ["hidden"]
>    end
>
>    mod = Hidden
>
>    p TextField.new.supported_types
>    p Hidden.new.supported_types
>
> __END__
>
> Output:
>
>    ["text", "password", "textarea"]
>    ["hidden"]

traits can be used in a similar way:

   harp:~ > cat a.rb
   require 'traits'

   class Element < Object
     class_trait 'supported_types'

     trait('supported_types'){ self.class::supported_types }
   end

   class TextField < Element
     supported_types %w[ text password textarea ]
   end

   class Hidden < TextField
     supported_types %w[ hidden ]
   end

   [ TextField, Hidden ].each{|klass| p klass => klass::new.supported_types }


   harp:~ > ruby a.rb
   {TextField=>["text", "password", "textarea"]}
   {Hidden=>["hidden"]}


cheers.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze.  --Nagarjuna
===============================================================================