Подтвердить что ты не робот

Ruby модули и классы с тем же именем в структуре

В одном из моих проектов у меня есть структура папок:

  • Lib
    • bar.rb
    • бар
      • other_bar.rb
      • another_bar.rb
      • next_bar.rb
      • ...

bar.rb

require File.expand_path(File.dirname(__FILE__) + "/bar/other_bar.rb")

class Bar
  puts "running BarBase"
end

бар /other _bar.rb

module Bar
  class OtherBar
    puts "running module Bar with class OtherBar"
  end
end

Если теперь я запускаю ruby bar.rb, я получаю следующее:

работающий модуль Bar с классом OtherBar
bar.rb: 3: in `': Bar не является классом (TypeError)

Я хотел бы иметь аналогичную структуру для структуры наследования модели rails. Как я могу это исправить? Насколько я знаю, рубин не поддерживает это из коробки. Есть ли обходной путь для такой ситуации?

4b9b3361

Ответ 1

Bar не может быть модулем и классом, это разные вещи.

Измените bar.rb на module Bar или измените other_bar.rb на class Bar.

Какой бы он ни был, он должен быть последовательным. Вы не можете изменить один на другой. Вопрос в том, что это должно быть? Если Bar является контейнером для других классов и имеет только несколько глобальных одноэлементных методов? Тогда a module. Но если он может быть создан, то он class.

И да, вы можете вложить классы. Это вполне приемлемо:

class Bar
  class OtherBar
    puts "running module Bar with class OtherBar"
  end
end

Bar::OtherBar.new # yay!

Модули и классы могут вставляться внутри любого другого любым способом, который вы считаете нужным.


Отредактируйте с помощью некоторых комментариев, чтобы помочь устранить все это:

module Foo

  # Foo::A
  class A
    # simple namespaced class
  end

  # Foo::B, inherits from Foo::A
  class B < A
    # inherting from a class in the same namespace
  end

  # modify Foo::B
  class B
    # When modifying an existing class you don't need to define the superclass
    # again. It will raise an error if you reopen a class and define a different
    # superclass. But leaving it off is fine.
  end

  # nested module Foo::Inner
  module Inner

    # Foo::Inner::C 
    class C
      # simple more deeply namespaced class
    end

    # Foo::Inner::D, inherits from Foo::A
    class D < A
      # inherits from a class in a parent namespace

      # works because ruby looks upward in the nesting chain to find missing constants.
    end

    # Foo::Inner::Foo
    class Foo
      # simple nested class with the same name as something in a parent namespace

      # This is a totally different Foo, because it in a different namespace
    end

    # Foo::Inner::E, inherits from Foo::Inner::Foo
    class E < Foo
      # class inhereting from another class in the same namespace

      # Foo::Inner::Foo is "closer" than the global Foo, so that gets found as the superclass
    end

    # Foo::Inner::F, which mixes in the gloabl module Foo
    class F
      # the :: constant prefix says to start looking in the global namespace
      # so here we include the top level module Foo, and not the "closer" in namespace Foo::Inner::Foo
      include ::Foo

      # This is an error. This attempts to include the class Foo::Inner::Foo since thats the closest by namespace
      # thing that matches the constant Foo. (you can't include classes, only modules)
      # You need the :: prefix to grab the global Foo module
      include Foo
    end

  end
end

# Z decalred in the global namespace, which inherits from the deeply nested class Foo::Inner::C
class Z < Foo::Inner::C
  # Any class anywhere can inherit from any other class in any namespace.
  # Just drill in!
end

# the following 2 declarations at this point would be identical

# This defines a class deep with in a namespace
class Foo::Inner::Foo::Bar < Foo::A
end

# same as above, but reopens each namespace
module Foo
  module Inner
    class Foo
      class Bar < ::Foo::A
      end
    end
  end
end

Ответ 2

Просто используйте class Bar вместо module Bar. В Ruby классы могут быть снова открыты и добавлены.