# Heirarchy Troubles

**URL:** <https://www.ruby-forum.com/t/heirarchy-troubles/47785>\
**Category:** Ruby\
**Created:** [November 13, 2005, 11:14pm UTC](https://www.ruby-forum.com/t/heirarchy-troubles/47785 "2005-11-13T23:14:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [November 13, 2005, 11:14pm UTC](https://www.ruby-forum.com/t/heirarchy-troubles/47785/1 "2005-11-13T23:14:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [November 13, 2005, 11:35pm UTC](https://www.ruby-forum.com/t/heirarchy-troubles/47785/2 "2005-11-13T23:35:31Z")

</div>

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/tut\_classes.html](http://www.whytheluckystiff.net/ruby/pickaxe/html/tut_classes.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).

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [November 14, 2005, 3:50pm UTC](https://www.ruby-forum.com/t/heirarchy-troubles/47785/3 "2005-11-14T15:50:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [November 14, 2005, 1:58pm UTC](https://www.ruby-forum.com/t/heirarchy-troubles/47785/4 "2005-11-14T13:58:49Z")

</div>

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
