インスタンス変数を大量に持つ大きなクラスを書くときは、==、eql? と書きます。ハッシュメソッドは大きな面倒です。このプロセスを自動化する「テンプレート クラス」を作成する方法はありますか? または他の方法で。
例:
class Template
def ==(other)
//Some way of comparing all of self's instance variables to other's.
end
def eql?(other)
self == other
end
def hash
//Some way of XORing the instance variables of self
end
end
class Test < Example
attr_reader :foo
attr_reader :bar
def initialize(foo, bar)
@foo = foo
@bar = bar
end
end
a = Test.new "123", "456"
b = Test.new "123", "456"
a == b
> true