From: Rick DeNatale Date: 2010-11-10T06:59:40+09:00 Subject: Re: YAML dependency On Tue, Nov 9, 2010 at 12:20 PM, Phillip Gawlowski wrote: > On Tue, Nov 9, 2010 at 4:02 PM, Guilherme Mello wrote: >> Hi, >> >> I'm creating a Gem and the code uses data from a YAML file: >> >> CONFIG = YAML.load_file("config/file.yml")["development"] >> >> This code is fixed in the class and located at Rails.root/config. The >> Gem looks to that path and loads the YAML. >> >> The question is: How allow the user to set what environment will be used >> outside of Gem ? > > Don't hard code the pathname, instead provide a default filename if > > myinstance = YourClass.new # or whatever makes sense for your gem > > doesn't pass a valid filename. Or check if you are running in Rails, > and then supply a default filename, or... Well from the OP I take it he is running in Rails due to the reference to Rails.root/config, and I think he's trying to emulate the kind of thing used to differentiate on the RAILS_ENV environment value in files like config/database.yml where the load produces a hash keyed on the environment. So, assuming the gem always is used in a Rails environment something like: CONFIG = YAML.load_file(Rails.root + "config/file.yml")[Rails.env] Rails.root returns a Pathname object, and Rails.env returns the value of ENV['RAILS_ENV'] which was the old school way before The Rails module got the env singleton method. -- Rick DeNatale Help fund my talk at Ruby Conf 2010:http://pledgie.com/campaigns/13677 Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale