I am creating what I expect to be ruby gem. Anyone have a good link to a tutorial on converting a simple library or plugin to a gem? Also, especially, what is the process that Ruby uses to allow the require to find gems? It seems to be something more than putting the files in the gem path (or is my configuration screwed up?).
Thanks
When you use the --user-install option, RubyGems will install the gems to a directory inside your home directory, something like ~/. gem/ruby/1.9. 1 . The commands provided by the gems you installed will end up in ~/.
Require reads the file from the file system, parses it, saves to the memory, and runs it in a given place. In require, if you modify the specified file when the script is running, those modifications won't be applied, Ruby will use the file from memory, not from the file system of the machine.
Gemfiles. Gemfiles require at least one gem source, in the form of the URL for a RubyGems server. Generate a Gemfile with the default rubygems.org source by running bundle init . If you can, use https so your connection to the rubygems.org server will be verified with SSL.
It's actually not to hard to do this manually. Let's say you have a library whatever.rb
that you want to distribute as a gem.
lib
and put a copy of whatever.rb
in lib/whatever.rb
.whatever.gemspec
, and put the following in there, filling in the appropriate values:
Gem::Specification.new do |spec|
spec.name = 'the-name-of-your-gem'
spec.version ='0.0.1'
# this is important - it specifies which files to include in the gem.
spec.files = ["lib/whatever.rb"]
# optional, but useful to your users
spec.summary = "A more longwinded description of your gem"
spec.author = 'Your Name'
spec.email = '[email protected]'
spec.homepage = 'http://www.yourpage.com'
# you did document with RDoc, right?
spec.has_rdoc = true
# if you have a ruby forge project
spec.rubyforge_project = 'your-project-name-on-rubyforge'
# if you have any dependencies on other gems, list them thusly
spec.add_dependency('hpricot')
spec.add_dependency('log4r', '>= 1.0.5')
end
gem build
command:% gem build whatever.gemspec Successfully built RubyGem Name: the-name-of-your-gem Version: 0.0.1 File: the-name-of-your-gem-0.0.1.gem %
gem install the-name-of-your-gem-0.0.1.gem
To use your library in a script then, simply do the following at the top:
require 'rubygems' # puts gem libraries in the require path
require 'whatever' # loads your library
For more on what the various settings in the gemspec
file, check the GemSpec Reference.
Personally, I use rubygems a lot to package executable scripts as well, and find it very handy for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With