From: Ross Bamford Date: 2005-12-21T14:12:48+09:00 Subject: Re: When are attr_reader type methods called? On Wed, 21 Dec 2005 04:09:33 -0000, Vivek wrote: > I couldnt get a proper subject for the post but here is my question. > Its regarding methods like > attr_reader defined like below > You can do this in ruby > > # file a.rb > class A > call_this ("hello world") > > def call_this > .... > end > end > > > The A::call_this is called immediately after I run the script.Becuase I > made the method print something and it printed it even though I hadnt > instantiated an object of the class with new. > What if the method uses some instance variables? wouldnt that be a > problem because its called even before an object is created?My question > is at what time during program execution are these methods called? If > there are more than one classes in a file then whats the order ? is it > defined by the language? It's top to bottom, as you'd expect. Does the following surprise you? NewClass = Class.new(Array) do attr_accessor :myattr def this "that" end end puts (nc = NewClass.new).this nc.myattr = 16 puts nc.myattr nc[3] = "Aha!" p nc I just made a new nuby file trying out the attr_accessor and instance var stuff you asked about: http://roscopeco.co.uk/code/noob/class-def.html -- Ross Bamford - rosco@roscopeco.remove.co.uk