[LIBRARY] 茁壮成长的ruby扩展库

自从rails红火以后, ruby的社区明显比以前壮大了不少(小弟我也是因为rails才学习ruby的),所谓人多好办事, 各种ruby的扩展类库也如雨后春笋一般冒出来,他们的特点就是版本低(很少上1.0), 名字都取得很有个性, 主要的目的也是增强或者替换ruby内置的标准库, 下面列出几个我在用的

rio : Ruby I/O Comfort Class

rio就是一个ruby内置io类的包装, 主要的作用范围就是操作文件, 抓网页, 压缩解压, 处理CSV等等IO操作, 现在的版本是0.3.7, rio使用起来及其简单
例子如下

拷贝或者增加内容到字符串对象中

 rio('afile') > astring      # copy
 rio('afile') >> astring     # append

拷贝或者增加内容到文件中 

 rio('afile') < astring      # copy
 rio('afile') << astring     # append

他的官方站点里面有详细的示例, 如果是经常要在linux上维护文件的朋友请不要错过.

commandline:命令行增强

commandline是一个简化命令行界面的库,他直接替代内置的optparser来解析用户使用的参数,同时还能自动打印出标准的usage/version/help提示信息,还提供Unix, Gnu, X Toolkit三种风格支持
使用commanline很简单,继承他的CommandLine::Application即可
例子如下

class App < CommandLine::Application

    def initialize
      version           "0.0.1"
      author            "Author Name"
      copyright         "2005, Jim Freeze"
      synopsis          "[-dhV] param_file out_file"
      short_description "A simple app example that takes two arguments."
      long_description  "app5 is a simple application example that supports "+
                        "three options and two commandline arguments."

      option :version
      option :debug
      option :help

      expected_args   :param_file, :out_file
    end

    def main
      puts "main called"
      puts "@param_file = #{@param_file}"
      puts "@out_file   = #{@out_file}"
    end
  end

通过执行ruby xxx.rb -h指令就可以立刻看到标准的help被打印出来

  NAME

      xxx.rb - A simple app example that takes two arguments.

  DESCRIPTION

      xxx.rb is a simple application example that supports three options
      and two commandline arguments.

  OPTIONS

      --version,-V
          Displays application version.

      --debug,-d
          Sets debug to true.

      --help,-h
          Displays help page.

  AUTHOR:  Author Name
  Copyright (c) 2005, Jim Freeze

fastercsv:更快更简单的csv库

顾名思义, 就是更快csv处理库, 偶看maillist上看到的评测是要快10倍(内置的实在太慢了,以至于fastercsv快它10倍还是比python版本的慢), 同时使用起来也比内置csv简单, 一看就会(除非你根本不知道csv是什么)
比如读取一个csv文件

  FasterCSV.foreach("path/to/file.csv") do |row|
    puts row[0]
    puts row[2]
  end

使用header支持读取cvs文件

  FasterCSV.foreach("path/to/file.csv",:headers=>true) do |row|
    #第一行是header,会自动忽略
    puts row['fielda']
    puts row['fieldc']
  end

需要提醒的是它使用require 'faster_csv',类名却是FasterCSV,不是很符合ruby的命名规则

转载于:https://www.cnblogs.com/baoz/archive/2006/03/07/2269851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值