Introduction
In my previous post I talked about interfaces. In this one I just do simple inheritance with the same classes as in the previous post. More difficult scenarios are possible but We should watch out with inheritance because it can get to complex very fast. Try to avoid complex inheritance trees.
Inheritance
class Plant
attr_accessor :name
end
class Animal < Plant
end
Yes Animals inherit from Plants because they share the same property so there.
And still running the same test.
``` require ‘rubygems’ gem ‘test-unit’ require “test/unit” require_relative “plant”
class Test_plant < Test::Unit::TestCase
def test_check_if_Plant_and_Animal_both_have_name my_plant = Plant.new my_animal = Animal.new my_plant.name = “test” my_animal.name = “test” my_array = [my_animal,my_plant] my_array.each { |x| assert_equal(“test”,x.name,“The name is not test”)} end end``` Which still turns green
and red like before.
Conclusion
Simple inheritance is not that difficult, but you can easily make it a lot more difficult. I think I have now the basis to start reading code others wrote and learn more from that.