Very simple ruby gem

I decided to create a simple gem to hold some date requirements that I had. Basically here is what you need to build a simple gem.

Create a working directory. In my case this was called fuzzydates. Change into the directory and create a lib directory so that you now have a directory structure that looks like this:

fuzzydates
fuzzydates/lib

For a very basic gem all we need is a gemspec file and the actual code in a .rb file. Firstly lets create the gemspec file in the base directory that you created (fuzzydates). Mine is called fuzzydates.gemspec

Gem::Specification.new do |s|
s.name = 'fuzzydates'
s.version = '0.0.1'
s.date = '2013-02-10'
s.summary = "a summary of your gem"
s.description = "describe your gem"
s.authors = ["your name"]
s.email = 'your email address'
s.files = ["lib/fuzzydates.rb"]
s.homepage = 'your website'
end

Paste the above text into the file and update the fields as required. The version number is up to you but will have a bearing on the version that gem sees or your rails application requires. The s.files section needs to contain the names of the .rb files you add to the lib directory. The s.name will be the name as your gem. Once update, save the file and then change to the lib directory that you created.

Once there create a file with the same name as you used in the s.files section above. In my case this was fuzzydates.rb

Now create a basic class and have it do something as a test. Mine just returns some dates.

class Fuzzydates
def self.end_of_this_month
return Date.current+1.month-Date.current.day
end
def self.first_of_next_month
Fuzzydates.end_of_this_month+1.day
end
end

Once you have saved this file return to the base directory where the .gemspec file is and run

gem build fuzzydates.gemspec

If all goes well it will build a .gem file and report the following (or similar)

Successfully built RubyGem
Name: fuzzydates
Version: 0.0.1
File: fuzzydates-0.0.1.gem

You can now install the gem using the normal gem install command from inside the directory where the .gem file is located.

There are other ways of using rvm and bundle to create the basic gem template but this works for me.