newby: class getter proper use help

Discussion in 'Ruby' started by weathercoach@gmail.com, Aug 15, 2007.

  1. Guest

    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.
    , Aug 15, 2007
    #1
    1. Advertising

  2. Guest

    Hi --

    On Thu, 16 Aug 2007, 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)
    , Aug 15, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Turin
    Replies:
    20
    Views:
    725
    Jerry Coffin
    Aug 8, 2008
  2. Lionel Thiry
    Replies:
    19
    Views:
    347
    Randy Kramer
    Mar 15, 2005
  3. Benoit Daloze

    Writing proper getter in a Ruby way

    Benoit Daloze, Jan 23, 2010, in forum: Ruby
    Replies:
    8
    Views:
    206
    Benoit Daloze
    Jan 24, 2010
  4. Leslie Cox

    newby could use a little help

    Leslie Cox, Nov 22, 2007, in forum: Javascript
    Replies:
    12
    Views:
    184
    Dr J R Stockton
    Nov 30, 2007
  5. Paul N

    Avoiding use of a getter

    Paul N, Jun 21, 2012, in forum: C++
    Replies:
    4
    Views:
    497
    Jorgen Grahn
    Jun 23, 2012
Loading...

Share This Page