Ruby – The Scripting Language
-
Upload
akio-tajima -
Category
Technology
-
view
4.471 -
download
3
description
Transcript of Ruby – The Scripting Language
Ruby – The Scripting Language
Akio Tajimaaka arton
Agenda
• decade• Scripting Language• Ruby• To Err is Human• Windows is Not Unix• Unix is Not United• For Your Pleasure
Rubyhttp://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=ruby%A4%C3%A4%C6%A1%FB%A1%FB%A1%FB
%A4%F2%C6%B0%A4%AB%A4%B9%A4%CE%A4%CB%C9%AC%CD%D7%A4%CA%A5%E4%A5%C4%A4%C0%A4%C3%A4%B1%A1%A9
2005 Rails を動かすのに必要なヤツ
2004 t Diary|portupgradeを動かすのに必要なヤツ
私と Ruby
• 1999 末 ~2000 年• Windows• ネットワーク• 正規表現• COM インターフェイス
• スクリプト言語
宣伝2000 2001
20092006 2008
Scripting Language
• The origin of the term was similar to its meaning in "a movie script tells actors what to do": a scripting language controlled the operation of a normally-interactive program, giving it a sequence of work to do all in one batch. For instance, one could put a series of editing commands in a file, and tell an editor to run that "script" as if those commands had been typed interactively. – http://www.c2.com/cgi/wiki?ScriptingLanguage
Ruby とは
• Ruby は、手軽なオブジェクト指向プログラミングを実現するための種々の機能を持つオブジェクト指向スクリプト言語です。本格的なオブジェクト指向言語 であるSmalltalk 、 Eiffel や C++ などでは大げさに思われるような領域でのオブジェクト指向プログラミングを支援することを目的としていま す。もちろん通常の手続き型のプログラミングも可能です。– http://www.ruby-lang.org/ja/about/
スクリプト
繰り返しの達人• 反復作業は達人を生む• 瞬時にファイル名のパターンを認識し• マシンのようにマウスを動かし• マシンのようにキーボードを叩く• マンマシンシステム
To Err is Human
To Forgive is Machine
• 何を繰り返しているか• 作業の分解
• ⇒ スクリプト化
例• t Diay のツッコミ spam
I ROBOT
• 2009/200902.tdcをエディタで開くTDIARY2.00.00Date: 20090204Name: メルMail: Last-Modified: 1139188007Visible: true
こないだ私のお兄さんとしたんですけど、不幸なことが.Date: 20090204Name: paealosMail: Last-Modified: 1140972441Visible: true
Viagra are often the first treatment tried for erectile .Date: 20090204Name: アリバイ会社Mail: Last-Modified: 1140972441Visible: true
<a href="http://dmajor.info/>アリバイ会社</a>.
削除
sudo –u www-data vi …
http://tach.arege.net/trac/browser/tdiary/trunk/contrib/util/clean-spam?rev=713
#!/usr/bin/env ruby## Copyright (C) 2004 Satoru Takabayashi <[email protected]># You can redistribute it and/or modify it under GPL2.#puts "Usage: tdiary-comment-clean PATTERN FILE..." if ARGV.length
== 0pattern = Regexp.new(ARGV.shift)file_names = ARGV
deleted_comments = []file_names.each {|file_name| i = File.open(file_name) first_line = i.gets
(continue…)
file_names.each {|file_name| i = File.open(file_name) first_line = i.gets
comments = [] comment = "" while line = i.gets if line == ".\n" comments.push(comment) comment = "" else comment << line end end i.close
(continue…)
tmp_name = "tmp.#{Process.pid}" File.open(tmp_name, "w") {|o| o.print first_line comments.each {|comment| if pattern.match(comment) deleted_comments.push(comment) else o.print comment o.puts "." end } } File.rename(file_name, file_name + ".bak") File.rename(tmp_name, file_name)}
deleted_comments.each {|comment| print comment puts "."}
Windows is Not
Unix
というわけで、 1.8.7-p173 にはバグが見つかってしまいました。ごめんなさい。修正したものをリリースします。
> > ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.gz> ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.bz2> ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.zip> > Checksums:> > MD5(ruby-1.8.7-p174.tar.bz2)= 88c45aaf627b4404e5e4273cb03ba2ee> SHA256(ruby-1.8.7-p174.tar.bz2)=
203978b6db1cc77a79ff03d141d162f6f17d86c3574f76de9eae9d0c8cb920bc> SIZE(ruby-1.8.7-p174.tar.bz2)= 4144807
[ruby-list:46090] Re: [ANN] Ruby 1.8.7-p174 released
無い袖は振れない
c:\TEMP>sha256sum ruby-1.8.7-p174.tar.bz2'sha256sum' は、内部コマンドまたは外部コマンド、操作可能なプログラムまたはバッチ ファイルとして認識されていません。
そうだ、ケーキを食べよう
@echo offc:\progra~1\ruby-1.9\usr\bin\ruby -x
/Users/arton/bin/sha256sum.bat %*@goto endofruby#!/usr/bin/rubyrequire 'digest/sha2'
def sha256sum(str) sha256 = Digest::SHA2.new sha256.update(str) sha256.hexdigestend
def file(name) File.open(name, 'rb') do |f| yield sha256sum(f.read) endend
if ARGV.length == 0 STDERR.puts( 'usage: ruby sha256sum.rb file [more
file...]‘)else ARGV.each do |a| file(a) do |sum| puts "#{sum} #{a}" end endend__END__:endofruby
c:\home\arton\ruby>sha256sum ruby-1.8.7-p174.tar.bz2203978b6db1cc77a79ff03d141d162f6f17d86c3574f76de9eae9d
0c8cb920bc ruby-1.8.7-p174.tar.bz2c:\home\arton\ruby>
余談:巨大なファイルdef file(name) File.open(name, 'rb') do |f| buff = 'x' * 16384 sha256 = Digest::SHA2.new while len = f.read(buff.size, buff) sha256 << buff 自動伸縮 end yield sha256.hexdigest endend
余談:速度比較 (470MB ファイル )
(一括読み込み版)c:\home\arton\ruby>sha256sum “Windows Mobile 6 Professional SDK Refresh.msi"ff2bccc6d9618ff64d8172e9612f7fc46342f9e055bf99b407e689b74fad321b Windows Mobile 6 Professional SDK Refresh.msi 16.723000 1.092000 17.815000 ( 22.539000)
(16KB 分割読み込み版 )c:\home\arton\ruby>sha256sum “Windows Mobile 6 Professional SDK Refresh.msi"ff2bccc6d9618ff64d8172e9612f7fc46342f9e055bf99b407e689b74fad321b Windows Mobile 6 Professional SDK Refresh.msi 17.020000 0.905000 17.925000 ( 17.749000)c:\home\arton\ruby>
いい時代だ……
-x-x[directory]• メッセージ中のスクリプトを取り出して実行しま
す。スクリプトを読み込む時に、 `#!' で始まり , "ruby" という文字列を含む行までを読み飛ばします。スクリプトの終りは EOF( ファイルの終り ), ^D( コントロール D), ^Z( コントロール Z) または予約語__END__ で指定されます。
• ディレクトリ名を指定すると、スクリプト実行前に指定されたディレクトリに移動します
Ruby リファレンスマニュアル
-x@echo offc:\progra~1\ruby-1.9\usr\bin\ruby -x /Users/arton/bin/sha256sum.bat %*
@goto endofruby#!/usr/bin/rubyrequire 'digest/sha2‘
……
end__END__:endofruby
FileUtils
• FileUtilscp –rrm –ftouch
ruby –rfileutils –e “FileUtils.touch Dir.glob(‘*.c’)”
wget
ruby –ropen-uri –e “File.open(ARGV[0], ‘wb’)do|f|f.write open(‘http://arton.hp.infoseek.co.jp/’ + ARGV[0]).read;end” ActiveRuby.msi
Unix Tools
• http://homepage1.nifty.com/~tetsu/ruby/• 渡辺哲也さんのスクリプト集– ls, cal, wc, sleep, mkdir, pkill, du, hd ……
Unix is not Linux
• start-stop-daemon
事例• Java の RMI サーバの起動シェル• 停止用シェルに与える pid ファイルが必要• Unix の Java は環境設定のために fork/exec• pkill …… × (すべてが java –Dxxxxx –Dxxxx
)• setpgrp java … ← シェルが止まらない または pid を残せ
ない
自前 start-stop-daemonif ARGV[0] == ‘stop' if File.exist?
(PIDFILE) File.open(PIDFILE,
'r') do |f| pid = f.read end begin Process.kill(-9,
pid.to_i) rescue end File.delete(PIDFILE) end
elsif ARGV[0] == ‘start' init_policy pid = fork if pid.nil? Process.setpgrp() exec(CMD) else File.open(PIDFILE,
'w') do |f| f.write(pid.to_s) end endend
まとめ• スクリプト言語はスクリプトを書くため
の言語– (他のことにも使える)
• スクリプト言語を使うということは、スクリプトを書くということ
• スクリプトを書くと手作業から解放される
• スクリプトを書こう!
Follow up
• 1.9.1 以降は、 fork ではなく spawn を使いましょう。 (thanks AKR and Yugui)
• ダイジェストを求めるには、ruby –rdigest/sha2 –e “p Digest::SHA2.file(‘filename’)”
で OK (thanks _tad_)例)C:\temp>ruby -rdigest/sha2 -e "p Digest::SHA2.file(ARGV[0])" ruby-1.8.7-
p174.tar.bz2#<Digest::SHA2:256
203978b6db1cc77a79ff03d141d162f6f17d86c3574f76de9eae9d0c8cb920bc>