Accessing a subclass

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

Florian Weber wrote:

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.

···

--
My email address is malte at gmx-topmail.de

"Florian Weber" <csshsh@structbench.com> schrieb im Newsbeitrag
news:1A38E5B7-C83D-11D8-B166-000A95BD142E@structbench.com...

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

Florian Weber wrote:

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