From: dblack@... Date: 2006-03-20T04:38:52+09:00 Subject: Re: simple ruby language question Hi -- On Mon, 20 Mar 2006, 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 > => # > 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 When using instance variables, you have to match each one precisely to the object whose instance variable it is. The @blah in @blah = 'YES!' belongs to the class object B. The @blah inside test belongs (or will belong, upon execution) to whatever object is calling "test". Unless that object is the class object B, its @blah will be different (even if it's an instance of B). So if you want them to match up, you have to call test on the class object B: B.test That will run "test" with B as the receiver, so you'll see B's instance variable @blah. (By the way, it's a good idea not to name your test methods "test", because there's already a method with that name in Kernel, so sometimes you'll get behavior you don't expect. That's not the issue here, though.) David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black