Eric Jacoboni wrote:
> 
> As Ruby beginner, i try some "canonical" OO scripts. Doing so, I've
> noticed some strange behaviour in inheritance mechanism (from my point
> of vue). Considering a base class Employee :
> 
> class Employee
>   attr_reader :name, :age, :salary
>   attr_writer :name, :age, :salary
>   def initialize(name, age, salary)
>     @name   = name
>     @age    = age
>     @salary = salary
>   end
>   def to_s
>     "Name: #{@name}, #{@age}, #{@salary}"
>   end
> end
> 
> Considering Manager, an Employee's subclass
> 
> class Manager < Employee
>   attr_reader :grade
>   attr_writer :grade
>   def initialize(name, age, salary, grade)
>      super(name, age, salary)
>      @grade = grade
>   end
>   def to_s
>    super + ", #{@grade}"
>   end
> end
> 
> A Manager "is a" Employee, reverse is not true (that's the classical
> assertion...)
> 
> In Ruby, it's legal to assign an Manager to an Employee with
> 
> anEmployee = aManager
> 
> anEmployee is now a Manager... 

No, aManager is just assigned to a variable named "anEmployee";
It is still an instance of the Manager class. The statement:

	anEmployee = aManager

simply assigns another name to the Manager instance referenced by
"aManager". The Employee instance that "anEmployee" once
referenced can no longer be referenced by that name. If
"anEmployee" was the only reference to that Employee instance,
then it will probably be garbage collected.

> I know Ruby doesn't care about type
> checking and i'm aware of the fact that both names are references,
> but, when it comes to classes, it hurts my inheritance conception. Is
> there a way to avoid this behaviour or is it a "feature" ?

The way to avoid confusion is to not assign objects to variables
that are named as if they were another object.
  
> --
> ?ric Jacoboni, n? il y a 1296955303 secondes

-- 
"It requires wisdom to understand wisdom:
 the music is nothing if the audience is deaf."
 - Walter Lippman