# Newby: class getter proper use help

**URL:** <https://www.ruby-forum.com/t/newby-class-getter-proper-use-help/112165>\
**Category:** Ruby\
**Created:** [August 16, 2007, 12:32am UTC](https://www.ruby-forum.com/t/newby-class-getter-proper-use-help/112165 "2007-08-16T00:32:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 16, 2007, 12:32am UTC](https://www.ruby-forum.com/t/newby-class-getter-proper-use-help/112165/1 "2007-08-16T00:32:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 16, 2007, 12:42am UTC](https://www.ruby-forum.com/t/newby-class-getter-proper-use-help/112165/2 "2007-08-16T00:42:00Z")

</div>

Hi –

On Thu, 16 Aug 2007, removed\_email\_address@domain.invalid wrote:

> the File.stat.  
> f = FileStats.new("/etc/services")  
> 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
