[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newby: class getter proper use help

weathercoach@gmail.com

8/15/2007 10:30:00 PM

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?



require 'etc'

class FileStats
def initialize(f)
@@s = File.stat(f)
@link = @@s.nlink
end

attr_accessor :user, :link

def user
begin
@user = Etc.getpwuid(@@s.uid).name
rescue
@user = @@s.uid.to_s
end
end

f = FileStats.new("/etc/services")
puts f.user
puts f.link


TIA. G.

1 Answer

David A. Black

8/15/2007 10:41:00 PM

0