Last active
August 29, 2015 14:02
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/hello.rb | |
module Hello | |
def say_hello | |
puts "Hellooo...." | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### 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.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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