accessor methods for class methods

  • Thread starter Jason Lillywhite
  • Start date
J

Jason Lillywhite

It seems odd that cattr_* (i.e. cattr_accessor, cattr_reader,
cattr_writer) is not in the native Ruby and that you have to require
active support and rubygems to get this to work. Including the rails
stuff appears to slow things down a little. Why is this not built in the
native Ruby source?
 
I

Iñaki Baz Castillo

El Viernes, 5 de Septiembre de 2008, Jason Lillywhite escribi=C3=B3:
It seems odd that cattr_* (i.e. cattr_accessor, cattr_reader,
cattr_writer) is not in the native Ruby and that you have to require
active support and rubygems to get this to work. Including the rails
stuff appears to slow things down a little. Why is this not built in the
native Ruby source?

Note that you can do the same using:

=2D-----------
class MyClass
=09
class << self
attr_accessor :class_attribute_1, :class_attribute_2
end

end
=2D----------


So you can use it:

=2D---------
MyClass.class_attribute_1 =3D something
=2D--------

=2D-=20
I=C3=B1aki Baz Castillo
 
D

David A. Black

Hi --

It seems odd that cattr_* (i.e. cattr_accessor, cattr_reader,
cattr_writer) is not in the native Ruby and that you have to require
active support and rubygems to get this to work. Including the rails
stuff appears to slow things down a little. Why is this not built in the
native Ruby source?

I can't answer that directly, but I can tell you why I'm glad it
isn't. It's partly that class variables are a bit oddball to start
with, and I'm not eager to see them used a lot more. But it's also the
terminology.

An "attribute" is, or should be, an attribute of an object. But class
variables are not object-specific; they're very promiscuous, visible
to a class, its instances, and all the subclasses and all their
instances.

Therefore, a class variable is not an appropriate choice for
representing an "attribute". The fit between instance variables, as a
language-level construct, and "attribute", as a concept, is very good;
but class variables are very different from instance variables, and
the "attr" terminology is very loose. (The methods may have uses, but
the names are problematic.)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
J

Jason Lillywhite

Thanks. That makes sense. I want to do what is simplest, but it seems
without cattr things would get worse. Here is what I'm trying to do:

I have one rb file used for inputs:

require 'rubygems'
require 'activesupport'
class Input
cattr_reader :y
def self.y
@y = 2
end
end

**except I have lots of vars instead of just :y and I'm assigning them
values here.

Then I go to a new rb file and do this:

require 'firstfile.rb'
class NewClass
def do_something
Input.y * 2.3
end
end

new = NewClass.new
p new.do_something

**The fact that I need to say "Input.y" seems very bulky. Is there a
better way to do this? Plus, if I get away from class vars, it will get
bulkier. any ideas?
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top