So you want to create a ruby gem? No problem, let's get started by creating a basic Lorem Ipsum generator.
Open up the 'lorem.gemspec' file and fill in the TODO's
Include in the gemspec your development dependencies like this:
Yes that's right, we want tests
Install dependencies by running:
Add the spec directory in the gem root
Create your rspec config file called '.rspec' in the project root with this content
--format documentation
Create the spec_helper.rb file in this directory with the following content
Now write your first test in 'spec/lorem_spec.rb'
describe Lorem do
describe '.ipsum' do
it "should return 'Lorem ipsum dolor sit amet...'" do
Lorem.ipsum.should include('Lorem ipsum dolor sit amet')
end
end
end
Now run your tests with:
And you should see it fail
Now implement the feature. Edit the lorem.rb file and in the Module place the following code:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent venenatis pellentesque sodales. Aliquam ullamcorper diam in metus consequat, ac elementum quam tincidunt. Suspendisse odio nibh, iaculis non turpis eget, bibendum facilisis lacus. Donec ultrices feugiat eros sed cursus. Pellentesque laoreet lacus ac tristique laoreet. Integer feugiat massa quis mi tempus porttitor. Morbi eget mauris arcu. In hac habitasse platea dictumst. Proin id risus sagittis, bibendum sem at, commodo est. Vivamus vestibulum nisi auctor justo fringilla consectetur. Etiam ultricies sapien nibh, ac fermentum dolor ornare malesuada. Quisque commodo purus eu convallis sollicitudin."
end
Now run your test again:
And the test should pass
Set the version number in your 'version.rb' file
Add to git with
And commit
Now install the gem with:
This will build the gem and install it locally
You are now ready to use your gem locally
In IRB you can do:
Lorem.ipsum
Or in rails app include in Gemfile:
When you are ready to share your gem with the world you can upload it to rubygems.org (after creating rubygems account and github account)
Now make something more useful!
No comments:
Post a Comment