attr_reader + attr_writer?

Discussion in 'Ruby' started by Joe Ruby MUDCRAP-CE, Oct 13, 2006.

  1. Is there a class method that combines attr_reader and attr_writer? I
    thought I remember seeing some "cattr_*" method, but don't know what
    that was.

    Thanks,
    Joe

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Ruby MUDCRAP-CE, Oct 13, 2006
    #1
    1. Advertising

  2. Joe Ruby MUDCRAP-CE

    Guest

    On Fri, 13 Oct 2006, Joe Ruby MUDCRAP-CE wrote:

    > Is there a class method that combines attr_reader and attr_writer? I
    > thought I remember seeing some "cattr_*" method, but don't know what
    > that was.


    class Foo
    attr_accessor :bar
    end

    foo = Foo.new

    foo.bar = 7
    puts foo.bar

    => 7



    Kirk Haines
     
    , Oct 13, 2006
    #2
    1. Advertising

  3. Joe Ruby MUDCRAP-CE wrote:
    > Is there a class method that combines attr_reader and attr_writer? I
    > thought I remember seeing some "cattr_*" method, but don't know what
    > that was.
    >
    > Thanks,
    > Joe


    I'm still new to this whole ruby and programming stuff, but doesn't
    attr_accessor do both of those?

    --
    Posted via http://www.ruby-forum.com/.
     
    Jeremy Woertink, Oct 13, 2006
    #3
  4. Joe Ruby MUDCRAP-CE

    jyzhang Guest

    attr_accessor defines both a writer and a reader

    cattr_* is for class variables... you know... the kind that looks like
    @@var

    James Zhang

    Jeremy Woertink wrote:
    > Joe Ruby MUDCRAP-CE wrote:
    > > Is there a class method that combines attr_reader and attr_writer? I
    > > thought I remember seeing some "cattr_*" method, but don't know what
    > > that was.
    > >
    > > Thanks,
    > > Joe

    >
    > I'm still new to this whole ruby and programming stuff, but doesn't
    > attr_accessor do both of those?
    >
    > --
    > Posted via http://www.ruby-forum.com/.
     
    jyzhang, Oct 13, 2006
    #4
  5. Joe Ruby MUDCRAP-CE

    Guest

    HI --

    On Fri, 13 Oct 2006, jyzhang wrote:

    > attr_accessor defines both a writer and a reader
    >
    > cattr_* is for class variables... you know... the kind that looks like
    > @@var


    Ruby doesn't have any cattr_* methods, though. Also, if you write a
    wrapper around class variables, it would be better to call them
    something else, as the "attr" in "cattr" implies that you're dealing
    with an object attribute. Since class variables are shared by many
    objects, they're not actually representing attributes.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
     
    , Oct 13, 2006
    #5
  6. Joe Ruby MUDCRAP-CE

    Marc Heiler Guest

    Jeremy Woertink wrote:
    > Joe Ruby MUDCRAP-CE wrote:
    >> Is there a class method that combines attr_reader and attr_writer? I
    >> thought I remember seeing some "cattr_*" method, but don't know what
    >> that was.
    >>
    >> Thanks,
    >> Joe

    >
    > I'm still new to this whole ruby and programming stuff, but doesn't
    > attr_accessor do both of those?



    yes, attr_accessor does this.

    I was also toying with an attr_reader that allows ? for
    read-vars ... ruby triggers crazy ideas. A haskell guy once said
    that he liked ? at end of method names in Ruby. ;-)

    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, Oct 13, 2006
    #6
  7. Joe Ruby MUDCRAP-CE

    Trans Guest

    wrote:
    > HI --
    >
    > On Fri, 13 Oct 2006, jyzhang wrote:
    >
    > > attr_accessor defines both a writer and a reader
    > >
    > > cattr_* is for class variables... you know... the kind that looks like
    > > @@var

    >
    > Ruby doesn't have any cattr_* methods, though. Also, if you write a
    > wrapper around class variables, it would be better to call them
    > something else, as the "attr" in "cattr" implies that you're dealing
    > with an object attribute. Since class variables are shared by many
    > objects, they're not actually representing attributes.


    To be clear cattr methods create both instance level and class level
    accessors using @@vars for storage. What would be a better name then
    class attribute?

    T.
     
    Trans, Oct 13, 2006
    #7
    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. Robert William Vesterman

    Forcing attr_writer to a type?

    Robert William Vesterman, Aug 27, 2005, in forum: Ruby
    Replies:
    4
    Views:
    167
    Malte Milatz
    Aug 28, 2005
  2. Ryan Hinton

    attr_writer variation

    Ryan Hinton, Jan 26, 2006, in forum: Ruby
    Replies:
    5
    Views:
    261
    Ryan Hinton
    Jan 27, 2006
  3. Joe

    Using attr_writer?

    Joe, Feb 26, 2006, in forum: Ruby
    Replies:
    5
    Views:
    124
  4. Markus Hohenhaus
    Replies:
    3
    Views:
    158
    Phrogz
    May 9, 2007
  5. aidy

    attr_writer and hash

    aidy, Jul 14, 2007, in forum: Ruby
    Replies:
    2
    Views:
    97
    Sharon Phillips
    Jul 14, 2007
Loading...

Share This Page