Using functionality in /lib in initializers


#1

I need to use some functionality that I’ve added to the Hash class in
an initializer. I’ve written the functionality into /lib/hash.rb;
however, rails apparently loads the files in /lib after the
initializers as the methods are not there to use in the initializer.

I’ve used a require statement at the top of the initializer to get the
functionality…
require File.join(RAILS_ROOT, ‘lib’, ‘hash’)

It works and no warnings are being thrown in the log; however, since
rails doesn’t load them until after the initializers, I wanted to
ask… Is this unsafe in some way?

– TW


#2

On Mar 19, 10:08 pm, tekwiz removed_email_address@domain.invalid wrote:

I need to use some functionality that I’ve added to the Hash class in
an initializer. I’ve written the functionality into /lib/hash.rb;
however, rails apparently loads the files in /lib after the
initializers as the methods are not there to use in the initializer.

Except perhaps in production mode, rails won’t magically load files
from lib for you (and event then that loading happens before
initializers run)

I’ve used a require statement at the top of the initializer to get the
functionality…
require File.join(RAILS_ROOT, ‘lib’, ‘hash’)

Just require ‘hash’ would do

Fred


#3

Ahh, I see! Thanks Fred!

– TW

On Mar 19, 5:25 pm, Frederick C. removed_email_address@domain.invalid