accessing a subclass

F

Florian Weber

hi!

im having two classes.. fruit and apple.

i wanna do something like

class Apple < Fruit
add_something "foobar"
end

in the fruit class i have a add_something method
which i want to save the "foobar" to a static hash
in the apple class. the hash however should also
not be declared in the apple class.

is this somehow possible? i guess not, or? just
making sure, since ruby definitely has a lot of suprises =)

i guess the only other way would be to make a static
hash in the fruit class which is 2d, the first dimension being
the subclass..

thanks!

ciao!
florian
 
M

Malte Milatz

Florian said:
class Apple < Fruit
add_something "foobar"
end

in the fruit class i have a add_something method which i want to save
the "foobar" to a static hash in the apple class. the hash however
should also not be declared in the apple class.

I'm not sure if I understood you correctly, but what about adding

class Apple
attr_accessor :the_hash
end

class Fruit
def add_something(obj)
# Using the_hash here
# Alternatively, use instance_variable_get:)"@the_hash")
# I haven't tested both, however
end
end

I don't think, however, that such a procedure would be desirable.
 
R

Robert Klemme

Florian Weber said:
hi!

im having two classes.. fruit and apple.

i wanna do something like

class Apple < Fruit
add_something "foobar"
end

in the fruit class i have a add_something method
which i want to save the "foobar" to a static hash
in the apple class. the hash however should also
not be declared in the apple class.

is this somehow possible? i guess not, or? just

It is:

class Fruit
def self.inherited(sub_class)
def sub_class.add_something(some, thing=true)
@somethings[some] = thing
end

sub_class.instance_eval { @somethings = {} }

# test code:
p sub_class
def sub_class.somethings; @somethings; end
end
end

class Apple < Fruit
add_something "foo"
end

p Apple.somethings
making sure, since ruby definitely has a lot of suprises =)

i guess the only other way would be to make a static
hash in the fruit class which is 2d, the first dimension being
the subclass..

Yeah, that's the other option. However, I guess storing it in the class
itself is better for performance and GC reasons.

Regards

robert
 
J

Joel VanderWerf

Florian said:
hi!

im having two classes.. fruit and apple.

i wanna do something like

class Apple < Fruit
add_something "foobar"
end

in the fruit class i have a add_something method
which i want to save the "foobar" to a static hash
in the apple class. the hash however should also
not be declared in the apple class.

is this somehow possible? i guess not, or? just
making sure, since ruby definitely has a lot of suprises =)

i guess the only other way would be to make a static
hash in the fruit class which is 2d, the first dimension being
the subclass..

You can make use of the fact that class methods are inherited, but class
instance variables are not:

class Fruit
class << self
def somethings
@somethings ||= {}
end
def add_something(thing)
somethings[thing] = true
end
end
end

class Apple < Fruit
add_something :spherical
end

p Fruit.somethings[:spherical] # ==> nil
p Apple.somethings[:spherical] # ==> true
 

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,148
Messages
2,570,834
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top