simple subclass question

  • Thread starter Ball, Donald A Jr (Library)
  • Start date
B

Ball, Donald A Jr (Library)

If I want a class and its children to have different values for the same
class variables, how would I go about making that happen?

- donald
 
R

Robert Dober

If I want a class and its children to have different values for the same
class variables, how would I go about making that happen?

- donald
Hi Donald, has been a long time...

504/4 > cat subclass-vars.rb && ruby subclass-vars.rb
#!/usr/bin/ruby
# vim: sts=2 sw=2 nu expandtab tw=0:
#
P = Class.new { @a = 42 }
class << P
attr_accessor :a
end

S = Class.new P

puts P.a
puts S.a
S.a = 43
puts P.a
puts S.a
------>
42
nil
42
43

Hopefully I understood what you wanted.

Cheers
Robert
 
D

Daniel Lucraft

Ball said:
If I want a class and its children to have different values for the same
class variables, how would I go about making that happen?

- donald

Instead of:

class A
def A.var=(v)
@@var = v
end
def A.var
@@var
end
end

class B < A
end

where A and B will share the class variable, use a class instance
variable instead:

class A
def A.var=(v)
@var = v
end
def A.var
@var
end
end

class B < A
end

where A and B will have different class instance variables.

You can use the attr_accessor notation to create class instance variable
getters and setters like this:

class A
class << self
attr_accessor :var
end
end

best,
Dan
 
R

Robert Dober

Thanks to you and Robert for the quick answers. I think I get it now; I
was a little fuzzy on the notion of class instance variables before. I
now use a construct like so:

class A
class << self
attr_accessor :var
end
end

class B < A
class << self
def var
@var || superclass.var
end
end
end

to allow subclasses's instance variables to default to their
superclass's until and unless overridden.

That was the point I missed, but of course you did that nicely :).
I'll admit the class << self
syntax continues to mystify somewhat; can anyone proffer an explanation
that would help me grok it fully?
I will take a chance by saying that

class << A
def x...
end
end

is the same as

class A
def self.x
end
end

although there might be some subtle differences

Do e.g, this
class << A
puts self
end
you can see kind of a Proxy object, but it behaves pretty much transparently.

Cheers
Robert
 
D

David A. Black

Hi --

Thanks to you and Robert for the quick answers. I think I get it now; I
was a little fuzzy on the notion of class instance variables before. I
now use a construct like so:

class A
class << self
attr_accessor :var
end
end

class B < A
class << self
def var
@var || superclass.var
end
end
end

to allow subclasses's instance variables to default to their
superclass's until and unless overridden. I'll admit the class << self
syntax continues to mystify somewhat; can anyone proffer an explanation
that would help me grok it fully?

Possibly; have a look at: http://www.rubypal.com/singletons.html


David

--
Upcoming Rails training by Ruby Power and Light:
Four-day Intro to Intermediate
May 8-11, 2007
Edison, NJ
http://www.rubypal.com/events/05082007
 

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,241
Messages
2,571,222
Members
47,856
Latest member
mmorais

Latest Threads

Top