Reputation: 578
I'm trying to create a gem with Bundler, following this guide: http://rakeroutes.com/blog/lets-write-a-gem-part-one/. In it, it says:
I incorrectly thought after taking my first look through the gemspec that I would need to add more require statements as I developed my gem. That isn’t the case: the files just need to be in git.
I am trying to clean up some of my old gems to use this convention, but when I install my gem, the classes from the other files are not available. I have a couple directories nested under my /lib dir, but I wouldnt think that would be an issue. Is there anything simple to overlook that would prevent my other files from being required? Any help would be appreciated.
Upvotes: 0
Views: 166
Reputation: 9094
In the link, when he says he doesn't need to add a lot of "require" statements, he must mean adding files to the s.files
, s.executables
, and s.test_files
arrays--these determine what files get packaged up into the gem and what files get ignored. As you can see from the gem spec, whatever's tracked by git in certain directories is going to be included in the packaged gem.
Ruby's require
is a different story. Standard require rules still apply.
Ruby's gem system works by adding a bunch of different places for Ruby to look for "foo.rb" when you run require "foo"
. If "lib"
is your only require path for your gem, when you require "my_gem"
Ruby is only going to run the code in lib/my_gem.rb. If lib/my_gem.rb doesn't require
any other files in your gem, then Ruby hasn't seen them and so you'll get undefined constant errors when you try to use the classes from those files.
For examples, you might take a look at two simple gems I've written; both were started with bundle gem
: HashToHiddenFields and SimpleStats. In both gems, main Ruby file in lib/ requires everything that needs to be loaded for the gem to work correctly. For example, hash_to_hidden_fields.rb requires action_view/helpers/hash_to_hidden_fields so that the ActionView::Helpers::HashToHiddenFields
constant+module exists so we can include it into ActionView::Base
.
Hope that answers the question. I know Ruby requiring was pretty fuzzy to me for a while.
Upvotes: 1