Log 1 创建自己的gem

背景:好奇gem包的用法,首先搞清楚什么是gem包。那我们就先来创建一个自己的gem包。

时间:2014-3-8

环境:Ubuntu + Ruby 1.9.3

记录:Roy


创建目录结构

$ cd ~;mkdir -p codes/ruby/sayhello
$ cd codes/ruby/sayhello
$ mkdir bin
$ mkdir lib

创建vim lib/sayhello.rb

 #!/usr/bin/ruby

 def sayhello(name)
puts "#{name} says hello to you!"
end

创建vim bin/sayhello

 #!/usr/bin/ruby
require 'sayhello'
param_num = 0
while param_num < ARGV.length
sayhello(ARGV[param_num])
param_num += 1
end

创建vim sayhello.spec

 SPEC=Gem::Specification.new do |s|
s.name = 'sayhello'
s.version = '0.1.0'
s.files = 'lib/sayhello.rb'
s.executable = 'sayhello'
s.summary = 'This is an example!'
s.description = 'A sample to say hello.'
s.email = 'burningroy2011@gmail.com'
s.homepage = "<link>"
s.authors = ['Roy']
end

sayhello目录结构

$ tree .
.
|
|-----bin
| |---sayhello
|-----lib
| |---sayhello.rb
|-----sayhello.spec

执行build

$ gem build sayhello.spec

安装sayhello包

$ gem install sayhello-0.1..gem
Successfully installed sayhello-0.1.
gem installed
Installing ri documentation for sayhello-0.1....
Installing RDoc documentation for sayhello-0.1....

已经安装的gem位置

$ cd /var/lib/gems/1.9./gems
$ tree .
.----sayhello-0.1.
|
|-----bin
| |---sayhello
|-----lib
|---sayhello.rb

使用包进行测试

创建vim ~/dash.rb

 #!/usr/bin/ruby

 require 'rubygems'
load Gem.bin_path('sayhello','sayhello')

执行~/dash.rb(注意打开执行权限)

$ ~/dash.rb Roy Lydia Osan
Roy says hello to you!
Lydia says hello to you!
Osan says hello to you!

在dash.rb中,我们通过load Gem.bin_path。去调用sayhello包中的bin/sayhello,并且bin/sayhello调用了lib/sayhello.rb。

Ruby - 创建自己的GEM的更多相关文章

  1. Ruby创建命令

    Ruby创建命令

  2. ruby 编译安装,gem国内源ruby.taobao.org

    centos6.6final 一.安装依赖包(使用默认CENTOS更新源): # yum install openssl* openssl-devel zlib-devel gcc gcc-c++ m ...

  3. ruby创建某些“关键字”方法别名的语法

    begin和end是ruby的关键字,但是Range中也有名称为begin和end的实例方法.现在问题来了:怎么创建它们的别名方法? 如果用class Range;alias begin_x begi ...

  4. RubyGems系列之创建自己的gem

    转载请注明来源:https://www.cnblogs.com/zhanggui/p/9720818.html 一. 前言 我们可以在rubygems.org中下载安装他人创建的gem.现在,我们尝试 ...

  5. 【Ruby】【改gem源镜像】【Win10 + Jruby-9.1.2.0 + Rails 5.1.3 + gem 2.6.4 】

    参考地址:https://ruby-china.org/topics/33843 (1)> gem sources --add http://gems.ruby-china.org 遇到问题: ...

  6. (转)Ruby On Rails 推荐 Gem 列表

    作者:尘缘,QQ:130775,来源:http://www.4wei.cn/archives/1002157 PHP的包管理Composer还在刚刚兴起的阶段,Ruby社区已经有很多成熟的Gem了,R ...

  7. ruby 制作自己的gem包

    在ruby工程目录下新建一个文件:crowdSystem.gemspec.需要在lib目录下存在同一名称的ruby库文件:crowdSystem.rb

  8. warning insecure world writable dir ruby mode 040777,gem insstal sass error failed to build gem native extension

    //1.删除原gem源 gem sources --remove https://rubygems.org/ //2.添加国内镜像 gem source -a https://gems.ruby-ch ...

  9. 关于解决ruby源码安装 gem install报错问题

    因做redis集群需要安装ruby,源码安装过后gem install redis安装redis接口报错 解决方案: 确保主机安装zlib,没有安装执行 yum -y install zlib zli ...

随机推荐

  1. k/3cloud表格控件块粘贴代码逻辑

    大家可以在表单插件EntityBlockPasting事件中自己处理,然后将cancel设置为true.以下代码可以参考一下,插件代码中需要将其中一些属性或方法修改,例如this.BusinessIn ...

  2. Python 列表的复制操作

    2013-10-18 10:07:03|   import copy a = [1,2,3,['a','b']] b = a c = a[:] d = copy.copy(a) e = copy.de ...

  3. 动态规划:HDU 1114 Piggy-Bank

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  4. 洛谷——P1451 求细胞数量

    P1451 求细胞数量 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.(1<=m,n<=10 ...

  5. [vijos1891]学姐的逛街计划

                                                                     学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, d ...

  6. Spring的@Qualifier注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-qualifier-an ...

  7. CSS 遮罩层、滑出页面

    <style> .panel_bak { position:fixed; bottom:0; display:none; width:100%; margin:0px; padding:5 ...

  8. [Rust] Setup Rust for WebAssembly

    In order to setup a project we need to install the nightly build of Rust and add the WebAssembly tar ...

  9. Redis 入门指南

    就是DBIdx

  10. WHU-1551-Pairs(莫队算法+分块实现)

    Description Give you a sequence consisted of n numbers. You are required to answer how many pairs of ...