From: Austin Ziegler Date: 2002-11-16T07:21:00+09:00 Subject: Re: Newbie questions regarding getter/setter methods... On Sat, 16 Nov 2002 05:27:19 +0900, christopher.j.meisenzahl@citicorp.com wrote: > Just working my way through the pickaxe book. > > If I have this class: > > class Song > def name; @name; end > def artist; @artist; end > def duration; @duration; end > end > > Do I also have to declare @name, @artist, and @duration within the > method? Or are they implicitly declared by declaring the accessor > methods? Ruby doesn't have declarations. An instance variable (@name, etc.) is created by use. Ruby will complain in a few situations if a variable is used without first being assigned, but that mostly has to do with local variables. > What about this case? > > class Song > attr_reader :name, :artist, :duration > end I tend to do this even at the beginning of my classes, even though all it does is create readers. In most cases, your instance variables will be created in the initializer of your class. -austin -- Austin Ziegler, austin@halostatue.ca on 2002.11.15 at 17.16.33