包含来自单独文件的Ruby类

时间:2022-08-03 23:20:04

For a while I had been including an entire class inside of a Ruby module. Apparently this is not what I am supposed to do. It appears that the point of a module is to store functions which can then be included as methods in a new class.

有一段时间我一直在Ruby模块中包含一个完整的类。显然这不是我应该做的。似乎模块的要点是存储函数,然后可以将这些函数作为方法包含在新类中。

I don't want this. I have a class that I want to keep in a separate file which I can access from other files. How can I do this?

我不想要这个。我有一个类,我想保存在一个单独的文件中,我可以从其他文件访问。我怎样才能做到这一点?

Thanks.

谢谢。

2 个解决方案

#1


55  

Modules serve a dual purpose as a holder for functions and as a namespace. Keeping classes in modules is perfectly acceptable. To put a class in a separate file, just define the class as usual and then in the file where you wish to use the class, simply put require 'name_of_file_with_class' at the top. For instance, if I defined class Foo in foo.rb, in bar.rb I would have the line require 'foo'.

模块作为函数的持有者和命名空间用于双重目的。将模块保存在模块中是完全可以接受的。要将类放在单独的文件中,只需像往常一样定义类,然后在要使用该类的文件中,只需在顶部放置require'name_of_file_with_class'即可。例如,如果我在foo.rb中定义了类Foo,则在bar.rb中我会让行需要'foo'。

If you are using Rails, this include often happens automagically

如果您使用的是Rails,这种情况通常会自动发生

Edit: clarification of file layout

编辑:澄清文件布局

#file: foo.rb
class Foo
  def initialize
    puts "foo"
  end
end

...

...

#file: bar.rb
require 'foo'

Foo.new

If you are in Rails, put these classes in lib/ and use the naming convention for the files of lowercase underscored version of the class name, e.g. Foo -> foo.rb, FooBar -> foo_bar.rb, etc.

如果您在Rails中,请将这些类放在lib /中,并使用命名约定来获取类名的小写下划线版本的文件,例如: Foo - > foo.rb,FooBar - > foo_bar.rb等

As of ruby version 1.9 you can use require_relative, to require files relatively to the file you are editing.

从ruby版本1.9开始,您可以使用require_relative来要求相对于您正在编辑的文件的文件。

#2


0  

You can also use load. Also you use require relative if the file is in the same directory. Read this link for further understanding: http://rubylearning.com/satishtalim/including_other_files_in_ruby.html

您也可以使用负载。如果文件位于同一目录中,也可以使用require relative。阅读此链接以进一步了解:http://rubylearning.com/satishtalim/including_other_files_in_ruby.html

#1


55  

Modules serve a dual purpose as a holder for functions and as a namespace. Keeping classes in modules is perfectly acceptable. To put a class in a separate file, just define the class as usual and then in the file where you wish to use the class, simply put require 'name_of_file_with_class' at the top. For instance, if I defined class Foo in foo.rb, in bar.rb I would have the line require 'foo'.

模块作为函数的持有者和命名空间用于双重目的。将模块保存在模块中是完全可以接受的。要将类放在单独的文件中,只需像往常一样定义类,然后在要使用该类的文件中,只需在顶部放置require'name_of_file_with_class'即可。例如,如果我在foo.rb中定义了类Foo,则在bar.rb中我会让行需要'foo'。

If you are using Rails, this include often happens automagically

如果您使用的是Rails,这种情况通常会自动发生

Edit: clarification of file layout

编辑:澄清文件布局

#file: foo.rb
class Foo
  def initialize
    puts "foo"
  end
end

...

...

#file: bar.rb
require 'foo'

Foo.new

If you are in Rails, put these classes in lib/ and use the naming convention for the files of lowercase underscored version of the class name, e.g. Foo -> foo.rb, FooBar -> foo_bar.rb, etc.

如果您在Rails中,请将这些类放在lib /中,并使用命名约定来获取类名的小写下划线版本的文件,例如: Foo - > foo.rb,FooBar - > foo_bar.rb等

As of ruby version 1.9 you can use require_relative, to require files relatively to the file you are editing.

从ruby版本1.9开始,您可以使用require_relative来要求相对于您正在编辑的文件的文件。

#2


0  

You can also use load. Also you use require relative if the file is in the same directory. Read this link for further understanding: http://rubylearning.com/satishtalim/including_other_files_in_ruby.html

您也可以使用负载。如果文件位于同一目录中,也可以使用require relative。阅读此链接以进一步了解:http://rubylearning.com/satishtalim/including_other_files_in_ruby.html