1

I have this code in an app/lib folder:

require 'yaml'

class YamlParser
  def parse
    data = YAML.load_file("#{Rails.root}/config/application.yml")
    require 'pry' ; binding.pry
    data[:production]
  end
end

YamlParser.new.parse

Why isn't it working? I am getting this error message:

uninitialized constant YamlParser::Rails (NameError)
davids
  • 6,259
  • 3
  • 29
  • 50
Jwan622
  • 11,015
  • 21
  • 88
  • 181

2 Answers2

1

Add this to your application.rb

config.autoload_paths += %W(#{config.root}/lib)
davids
  • 6,259
  • 3
  • 29
  • 50
  • but I put my file in app/lib which should be eagerly loaded right? – Jwan622 Apr 14 '15 at 14:19
  • 1
    nope, it doesn't as of Rails 3 http://stackoverflow.com/questions/4073856/rails-3-autoload – davids Apr 14 '15 at 14:23
  • No, I think it does load app/lib. I made an app/lib folder. The post is referring to just the straight Rails.root/lib folder. – Jwan622 Apr 14 '15 at 15:39
  • 1
    My fault! By `app/lib` I understood `project_folder/lib`. Anyway, I would encourage you to use `project_folder/lib`. – davids Apr 14 '15 at 16:52
1

Ah, my class YamlParser was located in a file called "yaml_parse.rb" and not "yaml_parser.rb". I am bad at naming convention.

Jwan622
  • 11,015
  • 21
  • 88
  • 181