On 22.03.2008 13:43, Robert Dober wrote:
> On Sat, Mar 22, 2008 at 11:34 AM, Robert Klemme
> <shortcutter / googlemail.com> wrote:
>> On 22.03.2008 09:34, Leon Bogaert wrote:
>>  > Ah thanks David.
>>  >
>>  > And that's of course why
>>  >
>>  > Module X
>>  >   instance_eval do
>>  >     @testing = [1,2,3]
>>  >   end
>>  > end
>>
>>  This sets an instance variable of X.
>>
>>
>>  > String.new('test').include(X)
>>
>>  'test'.include X
> Do you mean extend?

You are right, my answer was too short.  The bit I showed is indeed 
equivalent to the original - however both are dysfunctional. :-)

> And if so what good would that do, even if we had a reference to test
> as the module does not define any methods.

My point was that String.new "foo" is superfluous because "foo" does 
already create a new object.

> I am very confused by this thread

Please don't.  I would feel sorry to have caused confusion. :-)

> but somehow have the feeling that OP
> wants this

Actually we (I) do not exactly now what the OP exactly wants to do.  So 
far I know only that he wants to access instance variables from a 
module.  I am guessing that he thinks it's somehow different than from 
class instance methods.  But it isn't (apart from the initialization 
story, see previous posting).

A small addition: modules are a nice case where accessing instance 
variables via accessor methods instead of directly pays off because then 
you can centralize initialization:

# a bit silly example
module ItemManager
   def items
     @items ||= []
   end

   def add(item)
     items << item
   end

   def delete(item)
     items.delete item
   end

   def reorder
     items.sort!
   end
end

> but at least it clearly shows that the included or extended method
> that was defined in a module does precisely what OP doubted possible:
> Accessing ivars.

Correct.

Happy Easter

	robert