Add a class variable with a mixin

G

Gene Shuman

Is it easily possible to add a class variable to a class via a Mixin?
I'm running into insane problems. Can I access a Class's class
variables at all(outside of an instance), or are they otherwise private?
 
G

Gene Shuman

Actualy, I can do that. I just need to modify it externally afterwards
by name.
 
R

Robert Klemme

2010/1/28 Gene Shuman said:
Actualy, I can do that. =A0I just need to modify it externally afterwards
by name.

What exactly are you trying to do? Here's one way:

module Foo
attr_accessor :bar
end

String.extend Foo

irb(main):009:0> String.bar =3D "nonsense"
=3D> "nonsense"
irb(main):010:0> String.bar
=3D> "nonsense"
irb(main):011:0>

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

2010/1/28 Robert Klemme said:
What exactly are you trying to do? =A0Here's one way:

module Foo
=A0attr_accessor :bar
end

String.extend Foo

irb(main):009:0> String.bar =3D "nonsense"
=3D> "nonsense"
irb(main):010:0> String.bar
=3D> "nonsense"
irb(main):011:0>

PS: this is not a class variable in the "classical" sense, e.g. a
variable starting with "@@" (e.g. @@bar). It's an instance variable
of the class.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top