Hur man skapar en Ruby Gem

Ruby Gems or “gem” is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries. It is easy to manage and install to your system and it can be used by various rails applications.

Every RoR developer might have customised a gem at least once in his career, but not each one of them has actually, created a gem. Here, I am going to give a basic idea about how to create a new gem and how to publish it.

Kick off

Innan du börjar, se till att du använder den senaste rubyversionen. RVM är verkligen användbart i det här fallet;

How to name your Gem?

Don’t blindly give a random name to your gem. Please take care of the following;
Every bindestreck representerar en struktur (folder, module) immersion
Every understreck representerar en sammanfogning i klassnamnet

Some examples:

gem new_test_gem

  • kräver 'new_test_gem'
  • modul/klass NewTestGem

And:

gem gem-structure-new_test_gem

  • kräver 'gem/structure/new_test_gem
  • modul/klass Gem::Structure::NewTestGem

How to create your Gem?

Att skapa en pärla;
$ paket pärla new_test_gem
Det kommer att skapa några grundläggande filer och kataloger.

Where to add your code or job
Your beautiful code goes here
# lib/new_test_gem.rb

När du har lagt till din kod i lib-filen måste du uppdatera gemspec-filen;
# new_test_gem.gemspec

What is Gem Versioning?

# lib/new_test_gem/version.rb
module NewTestGem
VERSION = “0.1.0”
slutet

A couple of things about what you create

  • En ädelstensversion är en sträng med 3 nummer – 'XYZ'.
  • Ökar X, Y, Z

incrementing Z, a small change and it’s called the ‘build’ number(e.g. changing label/text) and no change in any functionality.
incrementing Y, a new functionality change and it’s called the ‘minor’ number. But no compatibility issues with the previously released versions.
incrementing X, severe change and it’s called the ‘major’ number and the change no longer compatible with the previous versions.
The gem below (Gem-release) helps you to version your gem with a simple ‘bump’ command.
Gem-release pärla:
https://github.com/svenfuchs/gem-release

How to build your Gem?

När vi väl har vår ädelstensspec måste vi bygga en pärla av den. Vi kan då installera det lokalt för att testa det.

Installation och testning
Once your gem is built, you will have to install it to your system.
And of course, you have to test it properly.
$ gem installation ./new_test_gem-0.1.0.gem

How to push your Gem?

För att få det gjort måste vi skapa ett konto på RubyGems.org.
Then, it will be available to the other rubyists all over the world through rubygems.org.
This is done by;
$ gem push new_test_gem-0.0.1.gem

skenor

För att kontrollera det läggs din pärla till i RubyGems.org
$ ädelstenslista -r new_test_gem

Slutet
That’s all it takes to create a gem!
If you want to create a Ruby Gem, you can further read through the following write-ups;
http://guides.rubygems.org/make-your-own-gem/
http://railscasts.com/episodes/135-making-a-gem
https://gorails.com/episodes/creating-gems-for-frontend-javascript-libraries

LUBAIB CEEJEY
Sr Ruby on Rails utvecklare

Prenumerera för de senaste uppdateringarna

relaterade inlägg

Lämna en kommentar

Din e-postadress kommer inte publiceras. Obligatoriska fält är märkta *

sv_SESwedish