📜 ⬆️ ⬇️

Meet the Gem. Part one

Good day!

Introduction


Every rubist, and indeed a programmer, sooner or later begins to think about writing their own libraries. He begins to bother dragging the same pieces of code into other projects. In Ruby, the cure for this disease is gem 's. So let's take a closer look at it.


Where to begin


For starters, it would be nice to find a dusty file with the extension .rb . Found? Ok, move on.
Next, we need to create a file called hello-world.gemspec , where, as you probably guessed, hello-world is the name of your future gem.
The next step will be to fill this file itself (if you can put it that way, the file specification). Its content should look like this:
Gem:Specification.new do |g| g.name = 'hello-world' #   gem'a g.version = '1.0' #    g.summary = 'This is the first gem in my life.' # ...    g.files = ['lib'/helloworld.rb'] #   g.author = 'krovatti' # , , ...    ... end 

Great, but we have nothing to collect! Why? We forgot to create helloworld.rb . Let's fix our mistake right away:
 class HelloWorld def initialize puts "Hello, World!' end end 

Fuuuuh! Now that we have a minimum set of files, we can start building our gem with you. To build it, we must use the command
 gem build hello-world.gemspec 

In case of successful execution of this operation, we will get a file with the name hello-world-1.0.gem . Everything, our gem is collected.
')

Wait a minute


Do you want to share your gem with other people? If so, you can easily do this with the following command:
 gem push hello-world-1.0.gem 

After executing this command, we should see the following:

  Pushing gem to RubyGems.org ...
 Successfully registered gem: hello-world (1.0) 

Alas, we will not see it. Do you know why? Because we are not yet registered with RubyGems. You can do it here .
Now repeat the push command and everything will be ok.

All over the world


Now our gem will be able to install any rubist (even from Australia) by executing the command
 gem install hello-world 

After the installation is complete, we write the following code and execute it:
 require 'rubygems' require 'hello-world' inst = HelloWorld.new 

As a result, we will see the coveted
  Hello, World! 
in our console.

Stop it!


Generally we traced on RubyGems. You have not forgotten about this? Delete our gem with the following command:
 gem yank hello-world -v 1.0 

Yes, and from our list of gem'ov can be removed. Played and that's enough!
 gem uninstall hello-world -v 1.0 


Conclusion


Here and the fairy tale is over, and who listened - well done.
Today we met with a truly wonderful tool called gem . Now you know that if something happens, the gem will surely come to your aid. And Chip, Chip, Chip ... And Dale is hurrying towards us ...

Source: https://habr.com/ru/post/134609/


All Articles