On Wed, 17 Aug 2005, Joe Van Dyk wrote:

> On 8/16/05, Ara.T.Howard <Ara.T.Howard / noaa.gov> wrote:
>> On Tue, 16 Aug 2005, Joe Van Dyk wrote:
>>
>>> module JoeAccessors
>>>  class << self
>>>    def joe_accessor_method *args
>>>      args.each do |arg|
>>>        eval <<-src
>>>          def #{ arg }
>>>            @#{ arg }
>>>          end
>>>          def #{ arg }= value
>>>            @#{ arg } = value
>>>          end
>>>        src
>>>      end
>>>    end
>>>  end
>>> end
>>>
>>> class Joe
>>>  include JoeAccessors
>>>  joe_accessor_method :arg1, :arg2
>>> end
>>>
>>>> test.rb:21: undefined method `joe_accessor_method' for Joe:Class (NoMethodError)
>>>
>>> *bangs head against desk*
>>
>> you're close - the issue is that including a module in another class/module
>> only inserts the __instance__ methods of the included module into the
>> class/module doing the reciever.  so, altough you've defined a class level
>> method correctly it doesn't make it into your new class.
>
> What's the rationale behind that?

i'm not sure - but if it were true to include class methods by default it'd be
easy to do this:

     harp:~ > cat a.rb
     module M
       module ClassMethods
         def new
           "can't create instances!"
         end
         def name
           "can't get the name"
         end
         def ancestors
           ['ha', 'ha']
         end
         def is_a?
           'nope'
         end
       end
       module InstanceMethods
       end
       def self::included other
         other.extend ClassMethods
         other.module_eval{ include InstanceMethods }
       end
     end

     class C
       include M
     end

     p C::new
     p C::name
     p C::ancestors
     p C::is_a?



     harp:~ > ruby a.rb
     "can't create instances!"
     "can't get the name"
     ["ha", "ha"]
     "nope"


if including modules added/supered class methods include would be almost
exactly like inheritence - some people think this is a good idea, some not.  my
take is just that, in the normal case it's not what you want - but sometimes it
is.

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
===============================================================================