Introducing Gem. Part one

    Good day!

    Introduction


    Every hacker, and indeed a programmer, sooner or later begins to think about writing their libraries. He begins to get bored of dragging the same pieces of code into other projects. In Ruby, gem 's are the cure for this disease . So let's get to know him better.


    Where to begin


    To start, it would be nice to find a dusty file with the extension .rb . Have you 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.
    Our next step will be to fill out this file itself (so to speak, a specification file). 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 so far we have nothing to collect! Why? We forgot to create helloworld.rb . Let's fix our mistake immediately:
    class HelloWorld
         def initialize
              puts "Hello, World!'
         end
    end
    

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

    If this operation is successful, the output will be a file called hello-world-1.0.gem . That's it, our gem is assembled.

    Wait a minute


    Do you want to share your gem with other people? If so, then 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 this. Do you know why? Because you and I 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 (at least from Australia) by running the command
    gem install hello-world
    

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

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

    Stop it!


    Actually, we inherited it 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 gems you can delete. Play and stop!
    gem uninstall hello-world -v 1.0
    


    Conclusion


    That’s the end of the tale, and whoever listened is well done.
    Today we met a truly wonderful tool called gem . Now you know that if something happens, gem will definitely come to your aid. And Chip, Chip, Chip ... And Dale hurries to us ...


    Also popular now: