maeshimaの日記

メモ書きです

"字句的に"囲っていることが定数探索の条件

Hoge::Fuga::Fooで定数Aを探すときに、Hoge::Fuga::Foo, Hoge::Fuga, Hogeの順で探すのかと思ってたけどそうではないみたいね

# -*- coding: utf-8 -*-
class Fuga
  def self.hoge
    puts "hoge"
  end
end


module Hoge
  module Fuga
    def self.hoge
      puts "fuga"
    end
  end
end

class Hoge::Fuga::Foo
  def self.hoge
    Fuga.hoge
  end
end

Hoge::Fuga::Foo.hoge # => "hoge"
# -*- coding: utf-8 -*-
class Fuga
  def self.hoge
    puts "hoge"
  end
end

module Hoge
  module Fuga
    def self.hoge
      puts "fuga"
    end
    class Foo
      def self.hoge
        Fuga.hoge
      end
    end
  end
end

Hoge::Fuga::Foo.hoge #=> "fuga"