attr_reader vs Objects and Attributes

Discussion in 'Ruby' started by Mike Onofrietto, Feb 10, 2011.

  1. Hello World.
    This is my first forum membership, and first post.
    I am learning Ruby.
    It seems to be a very elegant an encompassing OO language.
    I'm reading Thomas & Hunt: Classes, Objects, and Variables on-line.

    See attached.

    Regarding the following by the authors, and attached I see no
    correlation.
    However I've had very good results with attr_accessor.
    Please help.

    class Song
    def name
    @name
    end
    def artist
    @artist
    end
    def duration
    @duration
    end
    end
    aSong =3D Song.new("Bicylops", "Fleck", 260)
    aSong.artist =C2=BB "Fleck"
    aSong.name =C2=BB "Bicylops"
    aSong.duration =C2=BB 260
    ------------------
    class Song
    attr_reader :name, :artist, :duration
    end
    aSong =3D Song.new("Bicylops", "Fleck", 260)
    aSong.artist =C2=BB "Fleck"
    aSong.name =C2=BB "Bicylops"
    aSong.duration =C2=BB 260

    Attachments:
    http://www.ruby-forum.com/attachment/5878/TH_QA_1.doc
    http://www.ruby-forum.com/attachment/5879/attr.rb


    -- =

    Posted via http://www.ruby-forum.com/.=
    Mike Onofrietto, Feb 10, 2011
    #1
    1. Advertising

  2. Mike Onofrietto wrote in post #980715:
    > However I've had very good results with attr_accessor.
    > Please help.


    You only defined reader methods. You also need:

    def name=(name)
    @name=name
    end
    def artist=(artist)
    @artist=artist
    end
    ... etc

    'attr_accessor' gives you both reader and writer methods. 'attr_reader'
    gives you just the reader ones (as in your code), and 'attr_writer'
    gives just the writer ones.

    In future, it would be very helpful if you could post the exact error
    message you see. In your case it was probably something like
    NoMethodError - unknown method 'artist=', which would have immediately
    pinpointed the error.

    Regards,

    Brian.

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Feb 10, 2011
    #2
    1. Advertising

  3. On Thu, Feb 10, 2011 at 9:33 AM, Brian Candler <> wrote:
    > Mike Onofrietto wrote in post #980715:
    >> However I've had very good results with attr_accessor.
    >> Please help.

    >
    > You only defined reader methods. You also need:
    >
    > =A0def name=3D(name)
    > =A0 =A0@name=3Dname
    > =A0end
    > =A0def artist=3D(artist)
    > =A0 =A0@artist=3Dartist
    > =A0end
    > =A0... etc
    >
    > 'attr_accessor' gives you both reader and writer methods. 'attr_reader'
    > gives you just the reader ones (as in your code), and 'attr_writer'
    > gives just the writer ones.
    >
    > In future, it would be very helpful if you could post the exact error
    > message you see. In your case it was probably something like
    > NoMethodError - unknown method 'artist=3D', which would have immediately
    > pinpointed the error.


    I believe he did not get an error message but an empty (nil) output
    (other than he wrote):

    Ruby version 1.9.2
    irb(main):001:0> class Song; end
    =3D> nil
    irb(main):002:0> s =3D Song.new("Bicylops", "Fleck", 260)
    =3D> #<Song:0x10195a40>
    irb(main):003:0> s.instance_variables
    =3D> []

    At least that would have been the case with the code he pasted.

    Mike, your pasted code and output do not match. Also the code that
    you referenced is not the one in the attachment. Please make sure you
    report the real code and the real issue.

    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 10, 2011
    #3
  4. Mike Onofrietto wrote in post #980715:
    > Hello World.
    > This is my first forum membership, and first post.
    > I am learning Ruby.
    > It seems to be a very elegant an encompassing OO language.
    > I'm reading Thomas & Hunt: Classes, Objects, and Variables on-line.
    >
    > See attached.
    >
    > Regarding the following by the authors, and attached I see no
    > correlation.
    > However I've had very good results with attr_accessor.
    > Please help.
    >

    In your test code, and the attachment, you don't have an initialize
    function, which is defined in the first page of the book:

    i.e.

    class Song
    def initialize(name, artist, duration)
    @name = name
    @artist = artist
    @duration = duration
    end
    end

    The way Ruby works is that if this is defined, even if you have a new
    class block i.e.

    class Song
    def name; @name; end
    def artist; @artist; end
    def song; @song; end
    end

    that doesn't overwrite the previous definition. It EXTENDS it. So now
    your class should be (in memory anyway) equivalent to:

    class Song
    def initialize(name, artist, duration)
    @name = name
    @artist = artist
    @duration = duration
    end

    def name; @name; end
    def artist; @artist; end
    def song; @song; end
    end

    Without the initialization, you are called the base class
    Object.new(*arr) method, which will not assigned any class variables for
    you, regardless of whether you use attr_x methods or not.

    Mac.

    --
    Posted via http://www.ruby-forum.com/.
    Paul Mckibbin, Feb 10, 2011
    #4
  5. Brian,
    Thanks that helps.
    The code I pasted was from the Thomas & Hunt manual, but maybe I took it =

    out of context.
    Their presentation of the associated values further confused me.
    aSong.artist =C2=BB "Fleck"
    aSong.name =C2=BB "Bicylops"
    aSong.duration =C2=BB 260

    Yes - I was thinking attr_accessor was some combination of
    attr_reader, and attr_writer.
    Thanks again, Mike


    Brian Candler wrote in post #980745:

    >
    > You only defined reader methods. You also need:
    >
    > def name=3D(name)
    > @name=3Dname
    > end
    > def artist=3D(artist)
    > @artist=3Dartist
    > end
    > ... etc
    >
    > 'attr_accessor' gives you both reader and writer methods. 'attr_reader'=


    > gives you just the reader ones (as in your code), and 'attr_writer'
    > gives just the writer ones.
    >
    > In future, it would be very helpful if you could post the exact error
    > message you see. In your case it was probably something like
    > NoMethodError - unknown method 'artist=3D', which would have immediatel=

    y
    > pinpointed the error.
    >
    > Regards,
    >
    > Brian.


    -- =

    Posted via http://www.ruby-forum.com/.=
    Mike Onofrietto, Feb 12, 2011
    #5
  6. Robert,

    Thanks. The code, and output(?) I pasted, and attached are from the
    Thomas, and Hunt manual. The other attachment was some similar test code
    of mine.

    The issue was my lack of understanding as to why they wrote the methods
    without any initialization.

    Thanks again, Mike

    Robert Klemme wrote in post #980811:
    > On Thu, Feb 10, 2011 at 9:33 AM, Brian Candler <>
    > wrote:
    >> @artist=artist
    >> pinpointed the error.

    > I believe he did not get an error message but an empty (nil) output
    > (other than he wrote):
    >
    > Ruby version 1.9.2
    > irb(main):001:0> class Song; end
    > => nil
    > irb(main):002:0> s = Song.new("Bicylops", "Fleck", 260)
    > => #<Song:0x10195a40>
    > irb(main):003:0> s.instance_variables
    > => []
    >
    > At least that would have been the case with the code he pasted.
    >
    > Mike, your pasted code and output do not match. Also the code that
    > you referenced is not the one in the attachment. Please make sure you
    > report the real code and the real issue.
    >
    > Cheers
    >
    > robert


    --
    Posted via http://www.ruby-forum.com/.
    Mike Onofrietto, Feb 12, 2011
    #6
  7. Paul,

    Correct: they started that section with a fully initialized class, but
    later wrote it as I pasted it.

    Great example of extending the class, but I got the impression
    that they redefined the to_s method.
    Did they actually extend it too ?

    Thanks for the thorough response, Mike

    Paul Mckibbin wrote in post #980856:
    > Mike Onofrietto wrote in post #980715:
    >> Hello World.
    >> This is my first forum membership, and first post.
    >> I am learning Ruby.
    >> It seems to be a very elegant an encompassing OO language.
    >> I'm reading Thomas & Hunt: Classes, Objects, and Variables on-line.
    >>
    >> See attached.
    >>
    >> Regarding the following by the authors, and attached I see no
    >> correlation.
    >> However I've had very good results with attr_accessor.
    >> Please help.
    >>

    > In your test code, and the attachment, you don't have an initialize
    > function, which is defined in the first page of that section of the
    > book:
    >
    > i.e.
    >
    > class Song
    > def initialize(name, artist, duration)
    > @name = name
    > @artist = artist
    > @duration = duration
    > end
    > end
    >
    > The way Ruby works is that if this is defined, even if you have a new
    > class block i.e.
    >
    > class Song
    > def name; @name; end
    > def artist; @artist; end
    > def song; @song; end
    > end
    >
    > that doesn't overwrite the previous definition. It EXTENDS it. So now
    > your class should be (in memory anyway) equivalent to:
    >
    > class Song
    > def initialize(name, artist, duration)
    > @name = name
    > @artist = artist
    > @duration = duration
    > end
    >
    > def name; @name; end
    > def artist; @artist; end
    > def song; @song; end
    > end
    >
    > Without the initialization, you are calling the base class
    > Object.new(*arr) method, which will not assigned any class variables for
    > you, regardless of whether you use attr_x methods or not.
    >
    > Mac.


    --
    Posted via http://www.ruby-forum.com/.
    Mike Onofrietto, Feb 12, 2011
    #7
  8. Mac,

    I got it.
    Throughout the chapter the authors were merely extending the class from
    case to case just as you described.

    Take a look at my code. I extended the class twice:
    1st: to implement attr_reader "long hand"
    2nd: to implement attr_writer "long hand"

    I still have question which will wait.

    Thanks for your instruction, mikeonoff

    Attachments:
    http://www.ruby-forum.com/attachment/5908/extend_attr_longhand.rb


    --
    Posted via http://www.ruby-forum.com/.
    Mike Onofrietto, Feb 15, 2011
    #8
    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. Vivek
    Replies:
    3
    Views:
    121
    Patrick Hurley
    Dec 22, 2005
  2. Joe Ruby MUDCRAP-CE

    attr_reader + attr_writer?

    Joe Ruby MUDCRAP-CE, Oct 13, 2006, in forum: Ruby
    Replies:
    6
    Views:
    105
    Trans
    Oct 13, 2006
  3. Daniel Finnie

    attr_reader, etc for class variables

    Daniel Finnie, Dec 10, 2006, in forum: Ruby
    Replies:
    3
    Views:
    88
  4. libsfan01

    attr_reader explained

    libsfan01, Mar 3, 2007, in forum: Ruby
    Replies:
    5
    Views:
    99
    Eric Hodel
    Mar 5, 2007
  5. dkmd_nielsen
    Replies:
    6
    Views:
    220
    Joel VanderWerf
    Nov 26, 2008
Loading...

Share This Page