Where should I place my stone in my developing stone?

I'm trying to figure out where the stones should go. I wrote a ruby ​​script which I am trying to convert to a gem. I used a companion to create a skeleton for my gem.

Now I have 2 files "Gemfile" and "mygem.gemspec".

In my Gemfile I got 2 lines:

source 'https://rubygems.org'

# Specify your gem dependencies in cudruby.gemspec
gemspec

      

.. and in my "mygem.gemspec" I got:

# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mygem/version'

Gem::Specification.new do |spec|
  spec.name          = "mygem"
  spec.version       = Mygem::VERSION
  spec.authors       = ["mygem"]
  spec.email         = ["mygem@gmail.com"]
  spec.summary       = %q{mygem}
  spec.description   = %q{mygem}
  spec.homepage      = "http://www.mygem.com"
  spec.license       = "MIT"

  spec.files         = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md"]
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.6"
  spec.add_development_dependency "rake"
end

      

now my ruby ​​script is using 3 gems, "factoring", "debugger", "json".

Where can I add them? And How?

I found this: http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/

Now the guy says (from the link):

"When designing a gem, use the gemspec method in your Gemfile to avoid duplication. In general, the Gem Gemfile should contain the Rubygems source and one gemspec line ..."

So, I left my gemspec file as it is.

I checked out the official facter repository and its Gemfile: https://github.com/puppetlabs/facter/blob/master/Gemfile

.. and its gemspec: https://github.com/puppetlabs/facter/blob/master/.gemspec

now why does it have its gems in Gemfile and nothing in gemspec? It just does the exact opposite of what I'm reading.

Can anyone explain this to me please?

Thank!

+3


source to share


1 answer


The important detail is that Gemfile and mygem.gemspec are Ruby files. This means that you have a lot of flexibility and you can do things differently, provided that bundle

and gem

get the data they need. However, it's worth mentioning the standard examples from recent documentation, this code is usually cleaner and easier to track.

The method gemspec

in the Gemfile is a convenient way to tell Bundler that "this project uses the list of gems defined in the gemspec", which means it avoids the need to list dependencies twice to include bundle install

and allows the gem to set its dependencies.

Where to add your dependencies? At the end of the gemspec, for example:

  spec.add_development_dependency "rake"

  # Example of one of your dependencies
  spec.add_dependency "facter", ">= 2.3.0"
end

      

Ideally, you should specify a minimum version that you are sure will work without errors with your gem.



You shouldn't use the facter gem as a template or example, there is something non-standard going on there. For starters, the .gemspec link you are linking is not the factem gemspec used to assemble the gem. This stems at least in part from the gem's long history, which is almost 10 years old. When it was written for the first time, tools bundler

, and gem

was not the same as it is now.


It's not explicitly asked, but it's worth noting the completeness:

Dependency lists are for Bundler (in Gemfile) and build / install gem (in .gemspec). They don't automatically include dependents in your code. So you also need to add expressions require

to your ruby ​​source as you did before.

+2


source







All Articles