デザインパターンstrategy

strategyパターンです
ロジッククラスをポリモーフィズムで呼び出す


class Human

attr_accessor :name, :height, :weight, :age

def initialize(name, height, weight, age)
@name = name
@height = height
@weight = weight
@age = age
end
end

class AgeComparator

def compare(h1, h2)
if(h1.age > h2.age)
1
elsif(h1.age == h2.age)
0
else
-1
end
end
end

class MyClass

def initialize(comparator)
@comparator = comparator
end

def compare(h1, h2)
@comparator.compare(h1, h2)
end
end

m = MyClass.new(AgeComparator.new)
h1 = Human.new("human1", 170, 45, 25)
h2 = Human.new("human2", 190, 85, 18)
puts m.compare(h1,h2).to_s