Re: metaprogramming surprise?

Artur M. wrote:

class A
@@class_variable
@class_instance_variable
def initialize
@instance_variable
end
end

I hope this is the correct naming for this variables

That’s the way I think of them, too. It might help you to remember that
instance variables are just instance variables, regardless of where they
are stored. “Class instance variables” are just normal instance
variables that happen to be stored on a Class object. Nothing special
about them.

To achieve what you were trying you may want to look at the class
extensions in rails where there are simple class level accessors and
inheritable_class accessors that give each inherited class it’s own
copy of the accessors from the parent class.

http://dev.rubyonrails.org/svn/rails/branches/stable/activesupport/lib/active_support/core_ext/class/

On Wed, 18 Oct 2006, J2M wrote:

To achieve what you were trying you may want to look at the class
extensions in rails where there are simple class level accessors and
inheritable_class accessors that give each inherited class it’s own
copy of the accessors from the parent class.

http://dev.rubyonrails.org/svn/rails/branches/stable/activesupport/lib/active_support/core_ext/class/

attributes.rb is 42 lines of code and does all of that too, plus up
front or
deferred initialisation.

harp:~ > cat a.rb
require ‘rubygems’
require ‘attributes’

class A
class << self
attribute ‘a’ => 42
attribute(‘classname’){ self.name }
end
end

class B < A
end

p A.a
p B.a

p A.classname
p B.classname

harp:~ > ruby a.rb
42
42
“A”
“B”

-a

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs