“How do you test your own RubyGem?” “

What is a RubyGem

RubyGem is the standard source packaging format for the Ruby language.

Everyone has been using the gem command, but few people know how this thing comes from, here I scraped some information from the Internet to summarize, to share with you. You’ll find these links at the bottom, so if you want to know more, you can check them out. Ruby was heavily influenced by several other scripting languages, including Perl, and Perl had a CPAN (Comprehensive Perl Archive Network), which is like RubyGems.org today. But there was no such thing in Ruby at the time. Some organizations, such as CPAN and RubyGem, provide a free, public repository of source code packages, which are now used every day:

gem install rails

In the history of the development of RUBYGEM, there are several important figures, here also as the gossip of knowledge to you in the sun, as you talk about it after dinner. Everyone in the Ruby community probably knows Jim Weirich, who passed away in February 2014 as an adorable white-bearded uncle, He and four others, Rich Kilmer, Chad Fowler, David Black, and Paul Brannan, developed the basic specification and implementation of RubyGem in 2003, RubyGem was originally developed by Ryan Leavengood in 2001, but it didn’t catch on. Finally, in 2003, the first five people adopted the name RubyGem with the approval of Ryan Leavengood. A new version of RubyGem was developed, which does not use Ryan Leavengood’s code. Here attached rubygems executable files link, see comments, there are a few people on the name rubygems/blob/master/bin/gem

Rubygems have default source, can also change, https://rubygems.taobao.org is a basic for the domestic, some companies have their own requirements, set up their own private sources. The current https://rubygems.org is the official source, the source is also a preacher, early Ruby users know http://gems.rubyforge.org and http://gemcutter.org, and even making have all been used as the source, That is http://gems.github.com. All three are now deprecated.

See, a simple gem install history is quite a bit.

The basic usage of RubyGem

Gem install rails gem search rails gem search rails gem search rails gem search rails gem install rails gem search rails gem search rails gem search rails gem search rails gem search rails gem search rails gem search rails gem search Gem search ^rails -d gem search ^rails -d gem search ^rails -d gem search ^rails -d gem build package.gemspec Gem Push Pack-1.0.gem // The default is rubygems.org

This is just a brief list of the most common ways to use it. The command is very limited and very simple. Use gem –help and you can learn almost everything within 10 minutes.

How to make your own RubyGem

A few years ago there were tools like this and that, but now a Bundler is enough.

$ bundler gem mygem create mygem/Gemfile create mygem/Rakefile create mygem/LICENSE.txt create mygem/README.md create mygem/.gitignore create mygem/mygem.gemspec create mygem/lib/mygem.rb create mygem/lib/mygem/version.rb Initializing git  repo in /home/lizhe/Workspace/mygem

A Bundler command is done. Take a look at what’s in the mygem folder:

Total 24-RW-RW-R -- 1 Lizhe Lizhe 90 July 2 15:52 Gemfile drwxrwxr-x 3 Lizhe Lizhe 4096 July 2 15:52 lib-rw-rw-r -- 1 Lizhe License.txt-rw-rw-r -- 1 Lizhe Lizhe 250 July 2 15:52 mygem. Gemspec-rw-rw-r -- 1 Lizhe Lizhe 29 July 2 15:52 license.txt-rw-rw-r -- 1 Lizhe Lizhe 250 July 2 15:52 mygem 15:52 rakefile-rw-rw-r -- 1 lizhe lizhe 556 July 2 15:52 readme.md

Now look at the GemSpec file, which describes all the information about the Gem

# coding: utf-8 lib = File.expand_path('.. /lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include? (lib) require 'mygem/version' Gem::Specification.new do |spec| spec.name = "mygem" spec.version = Mygem::VERSION spec.authors = ["lizhe"] spec.email = ["[email protected]"] spec.summary = %q{TODO: Write a short summary. Required.} spec.description = %q{TODO: Write a longer description. Optional.} spec.homepage = "" spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0") spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "rake", "~> 10.0" end

What does ‘git ls-files-z ‘. Split (“\x0”) mean? And what is that \x0? Attach a link, explain, refer to the link. The first part of the Gem::Specification mainly describes the information of the Gem, including the name, version and so on. The second part is the file which the Gem contains, the execution file, and so on. Test files and which files under the path can be added to the load path. The third part is to develop the other gems that MyGem depends on. These information can be customized, first go by default. Let’s build the first gem.

$ rake build rake aborted! WARNING: See http://guides.rubygems.org/specification-reference/ for help ERROR: While executing gem ... (Gem::InvalidSpecificationException) "FIXME" or "TODO" is not a description / home/lizhe /. RVM/gems/ruby - 2.1.5 @ global/gems/bundler - 1.7.12 / lib/bundler/gem_helper rb: 149: ` in sh ' / home/lizhe /. RVM/gems/ruby - 2.1.5 @ global/gems/bundler - 1.7.12 / lib/bundler/gem_helper rb: 57: in ` build_gem ' / home/lizhe /. RVM/gems/ruby - 2.1.5 @ global/gems/bundler - 1.7.12 / lib/bundler/gem_helper rb: 39: ` in block in the install ' / home/lizhe /. RVM/gems/ruby - 2.1.5 @ global/bin/ruby_executable_hooks: 15: in ` eval ' / home/lizhe /. RVM/gems/ruby - 2.1.5 @ global/bin/ruby_executable_hooks: 15: in ` < main > 'Tasks: TOP => build (See full trace by running task with --trace)

This warning tells us that we need to replace our FIXME and TODO in GemSpec. It is not possible to build a gem without fixing this warning, so simply delete TODO in summary and description:

  spec.summary       = %q{Write a short summary. Required.}
  spec.description   = %q{Write a longer description. Optional.}

Let’s do it again:

$ rake build

mygem 0.0.1 built to pkg/mygem-0.0.1.gem.

Well, the first Gem was born. It’s right under the PKG of the current directory: mygem -0.0.0.gem. How to use it? Regardless of Bundler, if you start an IRB or PRY session, you will normally write: Require “mygem”. If you do this now, it won’t work because it hasn’t been installed in Ruby’s load path, so install it.

$rake install mygem 0.0.1 built to PKG /mygem-0.0.1.gem.

Now that it’s installed, let’s use it. Open the IRB:

require "mygem"
=> true
=> Mygem

The module is available, but the gem doesn’t do anything, so let’s add a method to it. Open lib/mygem. Rb and add a method:

require "mygem/version"

module Mygem
  def self.hello
    p "hello from my gem"

Save and then execute rake install. This command will first build, then install, and then reopen the IRB:

require "mygem"
=> true
=> "hello from my gem"

Now that it works, let’s release the first gem:

Rake release // Enter your account and password at rubygems.org

If you have a Rails application that just needs to output a hello from my gem, you can now add the gem to Gemfile:

gem 'mygem'

After adding the execution bundle install, you can use it in your Rails application.

Reference link:

  • RubyGems
  • Gemcutter Is The New Official Default RubyGem Host
  • Gem Building is Defunct

This article isOneAPMEngineer Li Zhe original article. Please visit the OneAPMOfficial Technology Blog.