logo       

Re: Sibling rivalry and class variables: msg#01198

lang.ruby.general

Subject: Re: Sibling rivalry and class variables

Hi --

On Tue, 13 Aug 2002, Nikodemus Siivola wrote:

> def class_attr_reader name
> eval <<-EOF
> def self.class_#{name}
> return @#{name}
> end
> EOF
> end
>
> class Parent
> class_attr_reader :info
> @info = "parent"
> def initialize
> puts "Parent 1: Initializing #{(self.class).class_info}"
> puts "Parent 2: in #{Parent.class_info}"
> end
> end
>
> class Daughter < Parent
> @info = "daughter"
> def initialize
> puts Daughter.class_info
>
> super
> end
> end

It's probably better not to hardcode the class's name into its method,
in case there's a GrandDaughter some day :-) You could do:

puts type.class_info

or, if you move that up into Parent, you can just have:

class Parent
class_attr_reader :info
@info = "parent"
def initialize
puts type.class_info
puts "Parent 1: Initializing #{(self.class).class_info}"
puts "Parent 2: in #{Parent.class_info}"
end
end

class Daughter < Parent
@info = "daughter"
end

class Son < Parent
@info = "son"
end


David

--
David Alan Black
home: dblack@xxxxxxxxxxxxxxxxxxxx
work: blackdav@xxxxxxx
Web: http://pirate.shu.edu/~blackdav




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise