52

I want to build an index for different objects in my Rails project and would like to add a 'count_occurences' method that I can call on String objects.

I saw I could do something like

class String
  def self.count_occurences
    do_something_here
  end
end

What's the exact way to define this method, and where to put the code in my Rails project?

Thanks

alex
  • 1,900
  • 3
  • 24
  • 34

3 Answers3

99

You can define a new class in your application at lib/ext/string.rb and put this content in it:

class String
  def to_magic
    "magic"
  end
end

To load this class, you will need to require it in your config/application.rb file or in an initializer. If you had many of these extensions, an initializer is better! The way to load it is simple:

require 'ext/string'

The to_magic method will then be available on instances of the String class inside your application / console, i.e.:

>> "not magic".to_magic
=> "magic"

No plugins necessary.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • Exactly what I needed, thanks! Just one question: why isn't the file loaded with "config.autoload_paths += %W(#{config.root}/lib)"? – alex Sep 20 '11 at 21:33
  • 2
    @alex: It would be, but you would need to restart your server or console, depending on what you're using to access it. The `autoload_paths` option configures directories containing files which will be automatically loaded, and so if it's *not* being loaded I suspect you've not done of those two things I just said. – Ryan Bigg Sep 20 '11 at 21:54
  • ok. all i needed to know! thank you so much. and oh, by the way, I just received Rails 3 in Action yesterday... looking forward to reading it! – alex Sep 20 '11 at 22:42
  • 4
    @RyanBigg: not sure why your instruction didn't work for me. Might be some set up issue. I am using Rails 3.0.7. here is what I did to make it work: 1. add config.autoload_paths += Dir["#{Rails.root}/lib/ext"] to config/application.rb 2. add require 'ext/string.rb' to the actual rails file in need of such custom String method. But thanks for your helpful posts. I got it to work! – GeorgeW Oct 03 '12 at 18:08
  • 2
    @RyanBigg thanks for the hint with the initializers. I put it into `config/initializers/load_class_extensions.rb` and require it from there now. – Daniel Oct 16 '13 at 09:10
  • 2
    doesn't work for me, it's something different in rails 5? – alex Jan 30 '17 at 22:17
  • 3
    @Daniel If you place the above code in `config/initializers/string.rb`, you don't even need to `require` it explicitly. See http://stackoverflow.com/a/5654574/3159183 – Seldom 'Where's Monica' Needy May 01 '17 at 13:46
14

I know this is an old thread, but it doesn't seem as if the accepted solution works in Rails 4+ (at least not for me). Putting the extension rb file in to config/initializers worked.

Alternatively, you can add /lib to the Rails autoloader (in config/application.rb, in the Application class:

config.autoload_paths += %W(#{config.root}/lib)

require 'ext/string'

See this: http://brettu.com/rails-ruby-tips-203-load-lib-files-in-rails-4/

Christopher Davies
  • 4,461
  • 2
  • 34
  • 33
6

When you want to extend some core class then you usually want to create a plugin (it is handy when need this code in another application). Here you can find a guide how to create a plugin http://guides.rubyonrails.org/plugins.html and point #3 show you how to extend String class: http://guides.rubyonrails.org/plugins.html#extending-core-classes

Ireneusz Skrobis
  • 1,533
  • 1
  • 11
  • 12
  • 1
    Is there a way I could accomplish the same thing without having to create a full plugin (which seems a lot of work for what I want to accomplish...)? – alex Sep 20 '11 at 20:34
  • 1
    I know the other solution that Ryan post and it is also correct. No doubt about it. But I recommended you plugin because this is what we do in our company. When you create a plugin you can put it on github and everyone can install it to his/her application. I know that this is more work but it is usually worth it. And I really don't feel that I deserved deducted point in here... – Ireneusz Skrobis Sep 21 '11 at 05:24