Topic: Best practices for core_extensions and custom libraries

Hello every one. I used to put all of my custom classes and core overrides in the config/initializers directory. Every thing worked fine but I read some articles like … ctory.html and came to the conclusion I should find a better way.

So I added the following to my application.rb

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
config.autoload_paths += Dir[Rails.root.join('lib', 'extensions')]
config.autoload_paths += Dir[Rails.root.join('lib', 'modules'

And added a file lib_loader.rb to the config/initializers directory that has all of the require statements

require 'relationship_synchronizer'
require 'hash'

In lib/modules their was a file relationship_synchronizer.rb

module RelationshipSynchronizer
# Bunch of irrelevant code

and in lib/extensions their was a file hash.rb

class Hash
class Hash
  #pass single or array of keys, which will be removed, returning the remaining hash
  def remove!(*keys)
    keys.each{|key| self.delete(key) }

  #non-destructive version
  def remove(*keys)

So now the question which has been making my brain hurt and I really hope some one can enlighten me on. I loaded up the rails console to check things out.

>> RelationshipSynchronizer
=> RelationshipSynchronizer

Ok looking good so far and

>> :remove
=> false


>> require 'hash'
=> true

>> :remove
=> true

Double WTF?

So now I am fairly confused. The require statement for RelationshipSynchronizer worked and did not work for hash. However if I used the same require statement later in the code or in the rails console it would load the new methods into the Hash class.

If any one has any insights into the matter I would be incredibly greatfull. Also if any one has more refiened practices for including custom non MVC code into rails apps that would be great to see as well.

Please and thank you and happy holidays.

Thank you

Last edited by tyger86 (2012-12-08 23:27:01)