Misha Moroshko
Misha Moroshko

Reputation: 171369

Where is the right place to put predefined class (e.g. String, Symbol) extensions in Ruby on Rails?

I would like to add my_method to the Symbol class, and be able to call my_method from app/helpers/application_helper.rb:

module ApplicationHelper
  def my_helper
    my_symbol.my_method
  end
end

Where is the most appropriate place to put:

class Symbol
  def my_method
    <some code here>
  end
end

?

Upvotes: 0

Views: 216

Answers (2)

Cory
Cory

Reputation: 2538

I typically create a file named monkey_patches.rb (or similar) - so it's very obvious where the patches are - then load it with an initializer in config/initializers. That's what they're for!

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163268

I think that sticking your native class extensions in a new file in your lib folder and require-ing them in your environment.rb file should do it.

Upvotes: 2

Related Questions