newby: class getter proper use help

W

weathercoach

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.
 
D

dblack

Hi --

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top