如何创建自己的ruby gem包

Python012

如何创建自己的ruby gem包,第1张

编写一个最简单的例子

1. 建好如下文件

注意:lib目录下必须有个和你gem名字一样的rb文件。

[ruby] view plaincopy

$ cd hola

$ tree

.

├── hola.gemspec

└── lib

└── hola.rb

2. 编写代码

. hola.rb

[ruby] view plaincopy

% cat lib/hola.rb

class Hola

def self.hi

puts "Hello world!"

end

end

.hola.gemspec

[ruby] view plaincopy

% cat hola.gemspec

Gem::Specification.new do |s|

s.name= 'hola'

s.version = '0.0.0'

s.date= '2010-04-28'

s.summary = "Hola!"

s.description = "A simple hello world gem"

s.authors = ["Nick Quaranto"]

s.email = '[email protected]'

s.files = ["lib/hola.rb"]

s.homepage=

'http://rubygems.org/gems/hola'

end

这里面可以设置很多属性。我会专门写篇文章介绍。

上面字段的意思,比较简单。相信大家都能理解。

3.编译生成gem

[ruby] view plaincopy

% gem build hola.gemspec

Successfully built RubyGem

Name: hola

Version: 0.0.0

File: hola-0.0.0.gem

% gem install ./hola-0.0.0.gem

Successfully installed hola-0.0.0

1 gem installed

4.测试使用

[ruby] view plaincopy

% irb

>>require 'hola'

=>true

>>Hola.hi

Hello world!

注意:在ruby 1.9.2之前到版本里面,需要先require 'rubygem',才能使用我们写的gem.

5.发布到rubygems网站

[ruby] view plaincopy

$ curl -u tom https://rubygems.org/api/v1/api_key.yaml >

~/.gem/credentials

Enter host password for user 'tom':

设定完之后发布

[ruby] view plaincopy

% gem push hola-0.0.0.gem

Pushing gem to RubyGems.org...

Successfully registered gem: hola (0.0.0)

发布成功。

这样任何一个人都可以使用你写的gem了。

稍微复杂的rubygem例子

上面的例子只有一个ruby文件,一般gem应该没有这么简单的。

下面说下有多个ruby文件该怎么写。

1. 目录结构

多了个hola目录和translator.rb文件

[ruby] view plaincopy

% tree

.

├── hola.gemspec

└── lib

├── hola

│ └── translator.rb

└── hola.rb

2. 代码

lib/hola/translator.rb

[ruby] view plaincopy

% cat lib/hola/translator.rb

class Hola::Translator

def initialize(language)

@language = language

end

def hi

case @language

when :spanish

"hola mundo"

else

"hello world"

end

end

end

lib/hola.rb

[ruby] view plaincopy

% cat lib/hola.rb

class Hola

def self.hi(language = :english)

translator = Translator.new(language)

translator.hi

end

end

require 'hola/translator'

.hola.gemspec

[ruby] view plaincopy

% cat hola.gemspec

Gem::Specification.new do |s|

s.name= 'hola'

s.version = '0.0.0'

s.date= '2010-04-28'

s.summary = "Hola!"

s.description = "A simple hello world gem"

s.authors = ["Nick Quaranto"]

s.email = '[email protected]'

s.files = ["lib/hola.rb", "lib/hola/translator.rb"]

s.homepage=

'http://rubygems.org/gems/hola'

end

红色是和上面不一样的地方。

其他步骤和上面一样了。很简单吧!

最后说下怎么写个 gem包含可执行文件的例子。

这个也很简单。像rake就是典型的包含可执行文件的gem.

1. 在刚才工程目录下建个bin文件夹

生成可执行文件,并且修改权限为可运行。

[ruby] view plaincopy

% mkdir bin

% touch bin/hola

% chmod a+x bin/hola

2. 修改可执行文件内容

bin/hola

[ruby] view plaincopy

#!/usr/bin/env ruby

require 'hola'

puts Hola.hi(ARGV[0])

测试下

[ruby] view plaincopy

% ruby -Ilib ./bin/hola

hello world

% ruby -Ilib ./bin/hola spanish

hola mundo

3 .最后修改gemspec

[ruby] view plaincopy

% head -4 hola.gemspec

Gem::Specification.new do |s|

s.name= 'hola'

s.version = '0.0.1'

s.executables <<'hola'

其他就和上面一样了。很简单吧。

一、新建文件

f=File.new(File.join("C:","Test.txt"), "w+")

f.puts("I am Jack")

f.puts("Hello World")

文件模式

"r" :Read-only. Starts at beginning of file (default mode).

"r+" :Read-write. Starts at beginning of file.

"w" :Write-only. Truncates existing file to zero length or creates a new file for writing.

"w+" :Read-write. Truncates existing file to zero length or creates a new file for reading and writing.

"a" :Write-only. Starts at end of file if file existsotherwise, creates a new file for writing.

"a+" :Read-write. Starts at end of file if file existsotherwise, creates a new file for reading and writing.

"b" :(DOS/Windows only.) Binary file mode. May appear with any of the key letters listed above

二、读取文件

file=File.open(File.join("C:","Test.txt"),"r")

file.each { |line| print "#{file.lineno}.", line }

file.close

三、新建、删除、重命名文件

File.new( "books.txt", "w" )

File.rename( "books.txt", "chaps.txt" )

File.delete( "chaps.txt" )

四、目录操作

1     创建目录

Dir.mkdir("c:/testdir")

04     #删除目录

05     Dir.rmdir("c:/testdir")

07     #查询目录里的文件

08     p Dir.entries(File.join("C:","Ruby")).join(' ')

10     #遍历目录

11     Dir.entries(File.join("C:","Ruby")).each {

|e| puts e

}

1、ARGV and ARGF

ARGV

ARGV <<"cnblogslink.txt"

#The gets method is a Kernel method that gets lines from ARGV

print while gets

p ARGV.class

ARGF

while line = ARGF.gets

print line

end

2、文件信息查询

#文件是否存在

p File::exists?( "cnblogslink.txt" ) # =>true

#是否是文件

p File.file?( "cnblogslink.txt" ) # =>true

#是否是目录

p File::directory?( "c:/ruby" ) # =>true

p File::directory?( "cnblogslink.txt" ) # =>false

#文件权限

p File.readable?( "cnblogslink.txt" ) # =>true

p File.writable?( "cnblogslink.txt" ) # =>true

p File.executable?( "cnblogslink.txt" ) # =>false

#是否是零长度

p File.zero?( "cnblogslink.txt" ) # =>false

#文件大小 bytes

p File.size?( "cnblogslink.txt" ) # =>74

p File.size( "cnblogslink.txt" ) # =>74

#文件或文件夹

p File::ftype( "cnblogslink.txt" ) # =>"file"

#文件创建、修改、最后一次存取时间

p File::ctime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009

p File::mtime( "cnblogslink.txt" ) # =>Sat Sep 19 08:06:34 +0800 2009

p File::atime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009

3、查找文件

puts "查找目录下所有文件及文件夹"

Dir["c:/ruby/*"].each {|x|

puts x

}

puts "条件查询"

Dir.foreach('c:/ruby') {

|x| puts x if x != "." &&x != ".."

}

puts "查找某一类型文件"

Dir["*.rb"].each {|x|

puts x

}

puts "Open 查询"

Dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}

puts "---------------------------"

puts "正则表达式查询"

Dir["c:/ruby/ruby/[rs]*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/[^s]*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/{ruby,li}*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/?b*"].each{|x| puts x}

puts "查找目录及子目录的文件"

require 'find'

Find.find('./') { |path| puts path }

3、查询目录及子目录文件

require "find"

Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|

Find.prune if f == "."

puts f

end

原型:ref.find( [ aName ]* ) {| aFileName | block }

prune:Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find::find.

4、文件比较 复制等

require 'ftools'

File.copy 'testfile', 'testfile1'  » true

File.compare 'testfile', 'testfile1'  » true