How to develop a rubygem of your own?

Source: Internet
Author: User

What is Rubygem

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

Everyone has been using gem this command, but very few people know how this thing came, here I get some information from the online summary, share to everyone. The last side will have these links, want to learn more, you can click to see. The Ruby language is heavily influenced by several other scripting languages, including Perl, and Perl has a CPAN (comprehensive perl Archive Network), which is like rubygems.org now, but Ruby did not have this Kind of a thing. Like CPAN and Rubygem, they are just a definition of a source of packaging and installation, and some organizations will provide this free public source package storage, which is now everyone to use the installation method:

gem install rails

In the history of the development of Rubygem, there are several important figures, here also as gossip knowledge for everyone to bask in, as we talk about the gossip point. Everyone in the Ruby community should know that Jim Weirich, who died in February 2014, is a lovely white beard, and he and the other four Rich Kilmer, Chad Fowler, David Black, Paul Brannan in 200 3 years set up the basic norms and implementation of Rubygem, but in fact Rubygem first Ryan Leavengood in 2001, unfortunately did not pop up, and finally to 2003 years, the front of 5 people after Ryan Leavengood agreed to use the Rub Ygem this name, developed a new version of Rubygem, which did not use Ryan Leavengood's code. Here is a link to the RubyGems's executable file, and a look at the note, with the names of the above several people Rubygems/blob/master/bin/gem

RubyGems has the default source, can also change, the basic domestic is https://rubygems.taobao.org , some companies have their own needs, will also build their own private sources. The current official source for https://rubygems.org This source is also several times, the early Ruby users know http://gems.rubyforge.org and http://gemcutter.org even GitHub have been used as a source, that is http://gems.github.com , these three are now deprecated.

Look, a simple gem install history is a lot of AH.

Basic methods of Rubygem use
gem install rails  //安装rails-v4.2.0   //安装指定版本的railsgem search rails  //查找所有名称中含有rails的gemgem search ^rails  //查找所有名称中以rails开头的gem-d  //查找所有名称中以rails开头的gem,并显示描述gem build package.gemspec  //构建一个gem,就是把你自己写的gem源码,打包成一个.gem文件gem push pack-1.0.gem  //发布到源上,默认是rubygems.org

Here is simply a list of the most commonly used methods, we look at the good, the command is very limited, also very simple, execution gem --help , basically all things you can learn in 10 minutes.

How to make a rubygem of your own

A few years ago there was such a tool, and now bundler it's 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/versionin /home/lizhe/Workspace/mygem

A bundler command will take care of it. Take a look at mygem what's under this folder:

Total --RW-RW- R-- 1Lizhe Lizhe -  7Month2  the: theGemfiledrwxrwxr- x 3Lizhe Lizhe4096  7Month2  the: theLib-RW-RW- R-- 1Lizhe Lizhe1062  7Month2  the: theLICENSE.Txt-RW-RW- R-- 1Lizhe Lizhe850  7Month2  the: theMygem.Gemspec-RW-RW- R-- 1Lizhe Lizhe in  7Month2  the: theRakefile-RW-RW- R-- 1Lizhe Lizhe556  7Month2  the: theReadme.Md

Now look at gemspec this file, which describes the Gem various information about this

# Coding:utf-8Lib = 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:writea  ShortSummary.  Required.} Spec.description =%q{todo:writeaLonger 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 do I mean when someone sees the contents of this file ‘git ls-files -z‘.split("\x0") ? And \x0 what is that? Attach a link to explain the reference link. At the top of this file, the Lib folder is added to the load path, and the first part of Gem::specification mainly describes the information of the gem, including the name, version, and so on, and the second part is which files are included in the gem, the files are executed, The test files and the files under which paths can be added to the load path. The third part is the development of other gems that mygem need to rely on. This information can be customized, followed by the default to go. Let's build the first gem.

$ Rake Buildrake aborted!WARNING:See http://guides.rubygems.org/specification-reference/for helperror:while executing gem ... (gem::invalidspecificationexception)"Fixme" or "TODO" is not a description/home/lizhe/.rvm/gems/[email Protected]/gems/bundler-1.7.12/lib/bundler/gem_helper.rb:149:in ' sh '/home/lizhe/.rvm/gems/[email protected]/gems/bundler-1.7.12/lib/bundler/gem_Helper.rb:57:in' Build_gem '/home/lizhe/.rvm/gems/[email Protected]/gems/bundler-1.7.12/lib/bundler/gem_helper.rb:39:in ' block in Install '/home/lizhe/.rvm/gems/[email protected]/bin/ruby_Executable_hooks:15:in ' eval '/home/lizhe/.rvm/gems/[email protected]/bin/ruby_Executable_hooks:15:in ' <main> ' tasks:top = build (see full trace by running task with--trace)

This warning is to remind us of the need to replace the gemspec FIXME and TODO , this warning if not resolved is unable to build a gem, directly delete the summary and description TODO :

  spec.summary       ashort summary. Required.}  spec.description   a longer description. Optional.}

To do it again:

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

Well, the first gem was born. It is in the current directory under the pkg: Mygem-0.0.1.gem. How to use it? Without considering bundler, if you open an IRB or pry session, it will generally be written like this: require "mygem" if you do this now, it certainly won't, because it hasn't been installed into Ruby's load path, then install it.

0.0.1 built to pkg/mygem-0.0.1.gem.mygem (0.0.1) installed.

Install it, then use it to open IRB:

require"mygem"trueMygem=> Mygem

Look, you can already use this module, but this gem can't do anything, so let's add a method to it, open lib/mygem.rb, add a method:

require"mygem/version"module Mygem  def self.hello    "hello from my gem"  endend

Save, then execute rake install , this command will build then install, and then reopen IRB:

require"mygem"true"hello from my gem"

To work properly, let's publish the first gem:

rake release// 输入你在rubygems.org上的账号和密码

If one of your rails applications just needs to output one hello from my gem , you can now add this gem to Gemfile:

gem ‘mygem‘

Once you've added the execution bundle install , you'll be able to use it in your Rails app.

Reference Links:

    • RubyGems
    • Gemcutter is the New official Default Rubygem Host
    • Gem Building is Defunct

This article is a ONEAPM engineer original article. ONEAPM is the emerging leader in China's basic software industry, helping enterprise users and developers easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the ONEAPM Official technology blog.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to develop a rubygem of your own?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.