It's got nothing to do with mixins:

class TestClass
  def x=(arg)
   @x = arg
  end
  def x
   @x
  end
  def monkey(arg)
   x = arg
  end
end 

irb(main):012:0> a = TestClass.new
=> #<TestClass:0x2ac6e48>
irb(main):013:0> a.monkey('aa')
=> "aa"
irb(main):014:0> a.x
=> nil

In the monkey method, x = arg is assigning to the local variable x, not
calling the method x=().

Refer to the x variable either as self.x or @x.


> -----Original Message-----
> From: Johannes Friestad [mailto:johannes.friestad / gmail.com] 
> Sent: Wednesday, 7 December 2005 4:07 PM
> To: ruby-talk ML
> Subject: Mixins and variables
> 
> Hi,
> I'm new to Ruby, and trying to figure out how the 
> inheritance/mixin works:
> I can't figure out how to set an instance variable with a mixin method
> from the object's initialize().
> 
> Example:
> -----------------
> module TestMod
>   def x
>     @x
>   end
>   def x=(arg)
>     @x=arg
>   end
> end
> 
> class TestClass
>   include TestMod
>   def initialize
>     x=('alpha')
>     printf("x=%s\n", x)
>   end
> end
> 
> irb(main)..>tmp=TestClass.new
> x=alpha               # x is set inside constructor
> => #<TestClass:0x37d9520>
> irb(main)..>tmp.x
> => nil                 # x is unset on the returned object
> -----------------
> 
> Does anyone know why it doesn't work? What can I do instead?
> 
> --
> Johannes Friestad
> johannes.friestad / gmail.com
> 
> 
> 
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################