Archive

Posts Tagged ‘Ruby’

瞎忙

September 4th, 2009 cashplk No comments

      最近新项目开始,难得老大重视。我也当上半个主程了。系统设计啊,业务熟悉啊,这次的项目就我最熟悉业务。所以也理所应当承担起大部分的业务逻辑。写了很多代码,挺高兴的。虽然很累。

      说到代码,第一次写这么多,还是看到了很多问题,关于代码的质量,如何抽取逻辑,提取方法。虽然以前看了很多东西,毕竟这次是理论结合实践了吧,项目已经提交测试了。每天还乐此不疲的进行代码的重构和修改。希望忙过这阵子,可以好好看看ruby和erlang。

Categories: work Tags: , , ,

使用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: , , ,

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