ruby

Ruby で grep モドキの実装

日本語対応の grep がほしいなと思ったので、Ruby でざっくり組んでみた。 require 'optparse' require 'kconv' regexp = ''; OptionParser.new {|opt| opt.on('-e VAL', '--regexp VAL') {|v| regexp = v.toutf8} opt.parse!(ARGV) } rule = Regexp.new(reg…

Ubuntu 10.04 で ruby-openid のサンプルを動かしてみた

Ubuntu 10.04 で ruby-openid のサンプルRPを http://gihyo.jp/dev/feature/01/openid/0002 を参考に動かしてみた。そのままの手順では動作しなかったため、 試行錯誤しながらようやく動作を確認できた。 インストール手順 必要なライブラリをインストール $…

サンプルプログラム

何のために書いたか忘れてしまったが、念のため残しておく。 class HogeNumber def initialize(v) @value = v end def *(other) @value * other end def coerce(other) if Integer === other then [other,@value] else [Float(other),Float(@value)] end end…

uniq コマンドの実装

ruby での実装、超簡易版。 temp = nil ARGF.each do |line| next if line == temp temp = line print line end

sort コマンドの実装(その2)

ruby で sort コマンドを実装。今回は "-r", "-n" オプションを実装してみた。 require 'optparse' class DefaultSort def compare(a, b) a <=> b end end class NumericSort def compare(a, b) /^(\d*|\s+\d+)(.*)$/ =~ a a1, a2 = $1, $2 /^(\d*|\s+\d+)(.…

sort コマンドの実装

超簡易版。 ARGF.sort.each {|v| print v}

ruby の組み込み定数 DATA

DATA を使うと、ファイルの __END__ 以後をデータとしてアクセスできるとのこと。DATA は、File オブジェクトらしい。erb などのテンプレートエンジンと組み合わせている例をどこかで見た。なかなか便利かも。 while line = DATA.gets print line end __END_…

comm コマンドの実装

ruby で組んでみた。 require 'optparse' visible1 = true visible2 = true visible3 = true opt = OptionParser.new opt.on("-1") {|v| visible1 = false} opt.on("-2") {|v| visible2 = false} opt.on("-3") {|v| visible3 = false} opt.parse!(ARGV) fnam…

Mongrel

Mongrel は、Ruby on Rails 用には最適なHTTPサーバらしい。 http://mongrel.rubyforge.org/静的なコンテンツの処理は苦手なので、Apache + Mongrel というのが 定番になっていくのかな?

cut コマンドの実装

ruby で組んでみた。対応しているのは、-f オプションと -d オプションのみ。 それ以外は使わないので。 require 'optparse' class Fields def initialize(rule) @ranges = [] rule.split(",").each do |v| if /^(\d+)?(-)?(\d+)?$/ =~ v b = $1 ? $1.to_i :…

コマンドライン引数でオプションを扱ってみる

optparse::チュートリアル を参考にする。

cat プログラムを書いてみる

3行でできた。たぶん1行でもいける。 while line = ARGF.gets print line; end