Forum: Ruby Heirarchy Troubles

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
CBlair1986 (Guest)
on 2005-11-14 00:14
(Received via mailing list)
Hello all. I've got a somewhat unique problem, I think. I have a list
of classes planned out, with the attributes needed for them and
everything else, but I want to figure out how the classes should relate
to one another, so that I don't need to needlessly repeat myself.

For instance, say I have the classes planned as such:
class Apple
  attr_accessor :taste, :type, :flavor, :flesh, ...
end
class Orange
  attr_accessor :taste, :type, :flavor, :peel, ...
end
and so on.

How would I go about fitting these together, so that I could have more
general classes such as Fruit and Vegetable and so on?

Thank you!
M.B.Smillie (Guest)
on 2005-11-14 00:35
(Received via mailing list)
On Nov 13, 2005, at 22:12, CBlair1986 wrote:

> Hello all. I've got a somewhat unique problem, I think. I have a list
> of classes planned out, with the attributes needed for them and
> everything else, but I want to figure out how the classes should
> relate
> to one another, so that I don't need to needlessly repeat myself.

This isn't unique at all, but is in fact one of the defining aspects
of all object-oriented design.  You can learn about it in the Pickaxe
here: http://www.whytheluckystiff.net/ruby/pickaxe/html/...


> For instance, say I have the classes planned as such:
> class Apple
>   attr_accessor :taste, :type, :flavor, :flesh, ...
> end
> class Orange
>   attr_accessor :taste, :type, :flavor, :peel, ...
> end
> and so on.

class Fruit
   attr :taste, :flavor, ...
end

class Apple < Fruit
   attr :something_unique_about_apples
end

class Citrus < Fruit
   def prevents_scurvy?
     true
   end
end

class Orange < Citrus
   # has everything from Fruit and Citrus and whatever's unique about
Oranges
end

class Lime < Citrus
   # has everything from Fruit and Citrus and whatever's unique about
Limes
end


For simple, every-day objects, this is a fairly straight-forward
process: Take an apple, and take an orange.  Make a list of the
properties of the apple you want to represent, and a similar list for
the orange.  When you want to represent the same thing for both of
the objects, make a superclass for them for those attributes/methods
(Fruit, Citrus).
Daniel S. (Guest)
on 2005-11-14 14:58
(Received via mailing list)
CBlair1986 wrote:
>   attr_accessor :taste, :type, :flavor, :peel, ...
> end
> and so on.
>
> How would I go about fitting these together, so that I could have more
> general classes such as Fruit and Vegetable and so on?
>
> Thank you!
>

class Fruit
   attr_accessor :type, :taste, :flavor

   def initialize(type = nil, taste = nil, flavor = nil)
     @type, @taste, @flavor = type, taste, flavor
   end
end

class Apple < Fruit
   def flesh
     # flesh the apple
   end
end

class Orange < Fruit
   def peel
     # peel the orange
   end
end
dblack (Guest)
on 2005-11-14 16:50
(Received via mailing list)
Hi --

On Mon, 14 Nov 2005, CBlair1986 wrote:

> Hello all. I've got a somewhat unique problem, I think. I have a list
> of classes planned out, with the attributes needed for them and
> everything else, but I want to figure out how the classes should relate
> to one another, so that I don't need to needlessly repeat myself.

That's unique?! :-)

> general classes such as Fruit and Vegetable and so on?
You could encapsulate the non-shared behaviors (or even the shared
ones, if you wanted) in modules:

   module Peelable
     attr_accessor :peel
   end

   module Fleshy
     attr_accessor :flesh
   end

   class Fruit
     attr_accessor :taste, :type
   end

   class Apple < Fruit
     include Fleshy
   end

   class Orange < Fruit
     include Peelable
   end

etc.  That way, if you had more than one peelable class, and the
nature of peelability changed, you could change it all in one place.


David
This topic is locked and can not be replied to.