Skip to content

Instantly share code, notes, and snippets.

@MyExperiments
Last active August 29, 2015 14:02
Show Gist options
  • Save MyExperiments/a47e1c86170de34a7958 to your computer and use it in GitHub Desktop.
Save MyExperiments/a47e1c86170de34a7958 to your computer and use it in GitHub Desktop.
Adding class/module to lib folder of rails application. By adding the lib folder to autoload_path, you don't need to require the file (require 'file') in other classes.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
module TestApplication
class Application < Rails::Application
#add the lib folder to autoload path
config.autoload_paths += %W(#{config.root}/lib/assets/ #{config.root}/lib/)
end
end
# lib/hello.rb
module Hello
def say_hello
puts "Hellooo...."
end
end
#### Access class placed in lib folder
2.0.0-p247 :001 > test = Test.new
=> #<Test:0xb490204 @x=12>
2.0.0-p247 :002 > test.x
=> 12
#### Access module placed in lib folder
2.0.0-p247 :004 > include Hello
=> Object
2.0.0-p247 :005 > say_hello
Hellooo....
# lib/test.rb
class Test
def initialize
@x = 12
end
def x
@x
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment