0

I am planning to use a Ruby module to add a function to the String class. http://snippets.aktagon.com/snippets/584-generating-word-n-grams-with-ruby

However, I don't know where to put Ruby modules so that every string object can use this function.

Where do I put modules in Rails?

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
Henley
  • 21,258
  • 32
  • 119
  • 207

2 Answers2

3

Methods like this often go in the config/initializers directory, ruby files contained in that directory are automatically required when Rails boots up.

Another option is within lib, but then they'll have to be explicitly required somewhere.

See the Rails guide on Configuring Applications for more information.

Daniel Vandersluis
  • 91,582
  • 23
  • 169
  • 153
  • 3
    Putting the guts in `lib` and then a one-liner `require 'thing-in-lib'` in `config/initializers` seems to be a pretty common pattern. – mu is too short Sep 10 '13 at 03:57
0

Put it in app/helpers with some name like common_methods.rb and then add it into your controller using require 'common_methods'. So that you can use the same function in all controllers.

Mulan
  • 129,518
  • 31
  • 228
  • 259
Kiran
  • 20,167
  • 11
  • 67
  • 99