ruby模块和类在结构中的名称相同

Mat*_*ias 26 ruby ruby-on-rails

我的一个项目中有如下文件夹结构:

  • 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
Run Code Online (Sandbox Code Playgroud)

酒吧/ other_bar.rb

module Bar
  class OtherBar
    puts "running module Bar with class OtherBar"
  end
end
Run Code Online (Sandbox Code Playgroud)

如果我现在跑,ruby bar.rb我得到这个:

运行模块Bar with class OtherBar
bar.rb:3:in`':Bar不是类(TypeError)

我想要一个类似于rails模型继承结构的结构.我怎样才能解决这个问题?据我所知,ruby不支持开箱即用.这种情况有解决方法吗?

Ale*_*yne 35

Bar 不能是一个模块和一个类,它们是不同的东西.

更改bar.rbmodule Bar或更改other_bar.rbclass Bar.

无论是哪种,都必须保持一致.你不能改变一个到另一个.问题是它应该是什么?If Bar是否是其他类的容器,只有少数全局单例方法?然后就是了module.但如果它可以被实例化,那么它就是一个class.

是的,你可以嵌套课程.这完全可以接受:

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

Bar::OtherBar.new # yay!
Run Code Online (Sandbox Code Playgroud)

模块和类可以以您认为合适的任何方式嵌套在其他模块中.


使用一些注释示例进行编辑以帮助清除这一切:

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's 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
Run Code Online (Sandbox Code Playgroud)

  • 嵌套不是继承.您或我发布的代码都没有任何继承.继承仅适用于类,而不是模块,并且在第一次将类声明为类Foo <Bar`时完成.不要混淆嵌套和继承,因为它们是完全正交的. (2认同)

mpa*_*tel 6

只需使用class Bar代替module Bar. 在 Ruby 中,类可以重新打开并添加到其中。