ruby学習メモ

  • モジュールの優先順位
# モジュール1の定義
module Sample1
  def test
    puts "Sample1!!"
  end
end

# モジュール2の定義
module Sample2
  def test
    puts "Sample2!!"
  end
end
class Clazz1
  include Sample1
  include Sample2
end
Clazz1.new.test
=> Sample2!!  # 最後に宣言した Sample2#test が呼び出される


class Clazz2
  include Sample2
  include Sample1
end
Clazz2.new.test
=> Sample1!!  # 最後に宣言した Sample1#test が呼び出される


class Clazz3
  def test
    puts "Clazz3!!"
  end
  include Sample2
  include Sample1
end
Clazz3.new.test
=> Clazz3!!  # クラス Clazz3 に定義された test が呼び出される
# スーパークラスの定義
class SuperClazz
  def test
    puts "superClazz!!"
  end
end
class Clazz4 < SuperClazz
end
Clazz4.new.test
=> superClazz!!  # スーパークラスに定義された test が呼び出される


class Clazz5 < SuperClazz
  def test
    puts "Clazz5!!"
  end
end
Clazz5.new.test
=> Clazz5!!  # クラス Clazz5 に定義された test が呼び出される


class Clazz6 < SuperClazz
  include Sample1
end
Clazz6.new.test
=> Sample1!!  # Sample1#test が呼び出される