Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > newby: class getter proper use help

Reply
Thread Tools

newby: class getter proper use help

 
 
weathercoach@gmail.com
Guest
Posts: n/a
 
      08-15-2007
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.

 
Reply With Quote
 
 
 
 
dblack@rubypal.com
Guest
Posts: n/a
 
      08-15-2007
Hi --

On Thu, 16 Aug 2007, http://www.velocityreviews.com/forums/(E-Mail Removed) 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoiding use of a getter Paul N C++ 4 06-23-2012 09:36 PM
Writing proper getter in a Ruby way Benoit Daloze Ruby 8 01-24-2010 07:02 PM
The use of const reference instear of getter Turin C++ 20 08-08-2008 02:28 PM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM
JSTL - Conditional Logic in a getter jc1771 Java 2 09-10-2003 10:41 AM



Advertisments