attr_reader + attr_writer?

  • Thread starter Joe Ruby MUDCRAP-CE
  • Start date
J

Joe Ruby MUDCRAP-CE

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
 
K

khaines

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
 
J

Jeremy Woertink

Joe said:
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?
 
J

jyzhang

attr_accessor defines both a writer and a reader

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

James Zhang
 
D

dblack

HI --

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 | (e-mail address removed)
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
 
M

Marc Heiler

Jeremy said:
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. ;-)
 
T

Trans

HI --



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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top