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 http://reefpoints.dockyard.com/ruby/201 … 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
end

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) }
    self
  end 

  #non-destructive version
  def remove(*keys)
    self.dup.remove!(*keys)
  end 
end
end

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

>> Hash.new.methods.include? :remove
=> false

WTF?

>> require 'hash'
=> true

>> Hash.new.methods.include? :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)