Gemfile vs Gemspec
We are not makers of history, We are made by history. — Martin Luther King, Jr.
As the quote said, let’s first understand how these two files came into existence. Once you know the past you can understand the present much better.
So It all started in 2006 when RubyGems created Gemspec to define Ruby package specifications. Gemspec is basically the Readme for gems. It tells you about the author, version, summary, description, internal dependencies, execution and everything about the Gem. So now it was easy to use gems as all of them were having their specifications with them. The confusion started with internal dependencies. In Gemspec file you define which gems your gem depends on. So how to figure out compatible versions of the gems which will be required to run your gem? How to manage same development environment across developers and deployments? There are other project which are not gems, how to manage dependencies in them? So some automated process was required to do this job. A process which can figure out compatible versions according to our configurations and can lock them for the deployment and production use.
To resolve this problem Bundler invented Gemfile in 2009. In Gemfile you just write the dependency name and it will give you the most compatible up to date version for your config. Whenever you want to change your dependency just change Gemfile and do `bundle install`, Bundler will handle rest of the things.
So Gemspec is basically information manual of your gem. It is necessary to have Gemspec to create a gem and Gemfile is optional. Although you can have Gemfile with Gemspec to track other dependencies apart from your gem. If you are not creating a gem you do not need Gemspec, you can simply use Gemfile to manage all your project dependencies.
It is good to know the uses of both Gemspec and Gemfile while working with ruby. Otherwise, You might end up in the struggle of dependency resolution due to having them in wrong files.