Archive

Archive for the ‘Ruby’ Category

使用Ruby分析文件&去重

June 24th, 2009 cashplk No comments

需要分析一个18M的文件,其中内容类似如下:

2009-06-20 00:00:07,678 [5409241381(T,105101,**,N,**,18ms)(12345678901234567890,dummy,**,888888,-,1000.00,**,-,005004-**,5409241381,0,null)]

文件以=== All done!!! ===结束。

全部日志加起来有7W多条,由于是时间程序批量请求的,所以其中肯定有很多重复的数据。我需要的只是那个12345678901234567890的20位数字而已。
so,尝试一下Ruby的脚本能力。

  file = File.open(result, 'r') # 读取文件
 
# 使用正则表达式匹配20个数字
regex = /\d{20}/
 
$array = Array.new();
 
  while(line = file.gets)  # 读取文件
    $array.push regex.match(line).to_s
    break if line == '=== All done!!! ==='
  end
  file.close
 
# 去重
$resultFile.puts($array.uniq)
puts 'convert ends!!'
Categories: Ruby Tags: , , ,

About: Ruby China Conf

May 25th, 2009 cashplk 1 comment

ruby conf china 2009的网站:

http://rubyconfchina.org/

演讲者以及对应的文档:

http://rubyconfchina.org/speakers

Categories: Ruby Tags: , ,

ruby操作CouchDB

May 23rd, 2009 cashplk No comments

以下是一个简单的使用Ruby操作CouchDB数据库的代码:

新建fromRuby2CouchDB.rb文件,具体内容如下:

require 'net/http'
 
module Couch
  class Server
    def initialize(host, port, option = nil)
      @host = host;
      @port = port;
      @option = option;
    end
 
    def delete(uri)
      request(Net::HTTP::Delete.new(uri))
    end
 
    def get(uri)
      request(Net::HTTP::Get.new(uri))
    end
 
    # put json to request
    def put(uri, json)
      req = Net::HTTP::Put.new(uri)
      req["content.type"] = "application/json"
      req.body = json
      request(req)
    end
 
    def post(uri, json)
      req = Net::HTTP::Post.new(uri)
      req["content-type"] = "application/json"
      req.body = json
      request(req)
    end
 
    def request(req)
      res = Net::HTTP.start(@host, @port) {
        |http|http.request(req)
      }
      if (not res.kind_of?(Net::HTTPSuccess))
        handle_error(req, res)
      end
      res
    end
 
    private
 
    def handle_error(req, res)
      e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:" +
 
          "#{req.method}\nURI:#{req.path}\n#{res.body}")
      raise e
    end
  end
 
end

在文件路径下,以命令行模式运行:irb,启动ruby编译器:
irb(main):001:0> require “fromRuby2CouchDB.rb”
=> true
irb(main):002:0> server = Couch::Server.new(“127.0.0.1″,”5984″)
=> #<Couch::Server:0xb7bdd258 @option=nil, @port=”5984″, @host=”127.0.0.1″>
irb(main):003:0> server.get(“/blog/”)
会插入一个新的数据库,名为blog。如果不对,会打印出相应的错误。成功后,可以在页面查看是否存在该数据库。

Categories: Erlang, Ruby Tags: , , ,

ruby惯用写法

May 23rd, 2009 cashplk No comments

放狗搜inject的时候找到的。
原帖:http://www.javaeye.com/topic/24642?page=1

Ruby有不少惯用法,这里略作一些介绍,也方便阅读他人代码:

迭代
一般写法:
for i in (1..10) puts i end

习惯写法:

(1..10).each{|i| puts i} 或 1.upto(10){|i| puts i}

||=赋值
一般写法:
number = 1 if number.nil? number = 1 unless number
习惯写法:

number ||= 1

Categories: Ruby Tags:

对于inject的使用

May 23rd, 2009 cashplk No comments

看到AlexAlex学Ruby[ inject需要注意的地方 ] ,节选部分如下:

inject([]) do |item ,i |这样的写法,每一步,item都会被设置为block的返回值。

如下:

1
2
3
4
5
6
7
8
9
    arr1 = []
    arr2 = [1,2,3]  
 
    arr2.each do |i|
        arr1 <<  i+1
    end
 
    p arr1
    #=> [2, 3, 4]

使用inject,则可以写为:

1
2
3
4
5
6
7
8
    arr2 = [1,2,3]
 
    arr = arr2.inject([]) do|arr1, i|
        arr1 << i + 1 
    end
 
     p arr1
     #=>[2,3,4]
Categories: Ruby Tags: ,

cashplk的心路历程 is Digg proof thanks to caching by WP Super Cache