48

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.

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.

user94154
  • 16,176
  • 20
  • 77
  • 116
  • This post clarifies the require_relative expression in case you are running into problems with the above example when on Ruby >=1.9.2 [Ruby require_relative example](http://stackoverflow.com/questions/3672586/what-is-require-relative-in-ruby) – Dirk May 09 '13 at 18:53

4 Answers4

79

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'.

If you are using Rails, this include often happens automagically

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.

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

orestiss
  • 2,183
  • 2
  • 19
  • 23
Ben Hughes
  • 14,075
  • 1
  • 41
  • 34
  • thanks, very helpful. could you clarify that last sentence? it seems you may have had a typo(?). If class foo is stored in bar.rb, i require 'foo' in bazcontroller.rb? Then foo.new is usable in bazcontroller.rb? – user94154 Jun 26 '09 at 19:22
  • 9
    Maybe it would be nice to add some info on 'require_relative' for Ruby 1.9 since the code doesn't work for this version. – boutta Jul 11 '12 at 13:25
  • 2
    @boutta Thank you for the 1.9 clarification, this fixed my issue. – DorkRawk Aug 15 '12 at 00:20
  • Answer saved the say for me. I had a class "SendGridHelper" in a file called "app/lib/sendgrid_helper.rb" I had to change the file name to "app/lib/send_grid_helper.rb". – mj_ Dec 11 '19 at 04:04
2

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

jhamezzz1315
  • 33
  • 1
  • 4
  • 12
  • Thanks, everyone wants to require classes, but I simply had some secret keys that I wanted to keep out of github. A simple `load secrets.rb` fits the bill perfectly! – axello Feb 11 '23 at 09:34
1

When Using Require, inside the string indicate the full path name of the class you are refereing to unless its in the Root Folder of Ruby

0
MyApp
|_ app
|_ bin
|_ etc, etc
root_level_file.rb

You can just do: require './root_level_file'.

RootLevelFile.new

JosephA91
  • 487
  • 3
  • 10