— 環境 — Ruby 2.2.2 Rails 4.2 ※ 2015/11/12 記事の末尾に追記しました。 リファクタリング前のコード 単純化してますけど、具体的には以下のようなサンプルで、最初に書いたのが次のようなコード。 class Person def initialize(name, age) @name = name @age = age end def get_old @age += 1 end end class Female < Person MARRIAGE_AGE = 16 def marriageable? @age >= MARRIAGE_AGE end end class Male < Person MARRIAGE_AGE = 18 def marriageable? @age >= MARRIAGE_AGE end end taro = Male.new('
![Rubyで親クラスから子クラスの定数を参照](https://cdn-ak-scissors.b.st-hatena.com/image/square/b27ba1d703068fc4853ea1271440cbb779e7cabb/height=288;version=1;width=512/https%3A%2F%2Feasyramble.com%2Fwp-content%2Fuploads%2F2016%2F03%2Feasyramble_ogp.jpg)