From: dblack@... Date: 2007-08-16T07:41:03+09:00 Subject: Re: newby: class getter proper use help Hi -- On Thu, 16 Aug 2007, weathercoach@gmail.com wrote: > Hello. > I'm an aspiring ruby hobbyist. I've finally got my head around > some basic class usage and use of setter/getter methods. However > I've recently bumped into the threshold of my understanding. My query > here relates to trying to further my understanding of classes and > class methods. > > In the following example. Defining the instance variable "@link" > does > not require anything beyond associating it with the nlink value from > the File.stat. > > class FileStats > def initialize(f) > @@s = File.stat(f) > @link = @@s.nlink > end > > attr_accessor :link > end > > f = FileStats.new("/etc/services") > puts f.link > > In this example the infomation I want cannot be simply retreived > from a subset of the File.stat so I create a method called "user". > This method provides the warning "warning: method redefined; > discarding old user" during execution because I already defined the > "user" method with my attr_accesor line. So I think my questions > are. > > 1. Am I over thinking this and I should just remove the "user" > method > from attr_accessor? > 2. Should I include my "user" method within the "initialize" > method? You can do: attr_writer :user and that leaves you to write the reader method yourself. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)