MichaelEconomy / gmail.com wrote:
> ok, i've got some module and it has some instance variables i want to
> be set by the classes extending the module, and aparently i'm not doing
> this correctly:
> 
> 
> module A
>   def test
>     raise 'ARG' if !@blah
>     @blah
>   end
> end
> 
> class B
>   include A
>   extend A
>   @blah = 'YES!'
> end
> 
> 
> 
> irb(main):025:0> b = B.new
> => #<B:0x136376c>
> irb(main):026:0> b.test
> RuntimeError: ARG
>         from (irb):9:in `test'
>         from (irb):26
> 
> 
> can anyone tell me what the syntax should be? how do i set @blah in the
> B class so that its available to the test method
> 

Try this one:

module A
   def test
     raise 'ARG' if !@blah
     @blah
   end
end

class B
   include A

   def initialize
     @blah = 'YES!'
   end
end

b = B.new
p b.test

Module#include will make module methods to become instance ones. 
Object#extend adds methods to an instance (to the class B in this case, 
you could say then B.test)

consider also:

class A
     @a=4
end

p A.class_eval{@a}
=> 4

which means that @a is an instance variable of class object, not an 
instance variable of class instance.


lopex