トップ «前の日記(2009年03月09日) 最新 次の日記(2009年03月29日)» 編集

Masa's blog

検索キーワード:

2009年03月13日 ruby(1.8.x)儂(わし)的解釈によるメモ [長年日記]

_ ruby(1.8.x)儂(わし)的解釈によるメモ(基本編)

使われている用語等は基本的にテキトーなもの。あまり真に受けないように。あくまで儂(わし)的なrubyプログラミングで困らない程度のTipsの蓄積。

#! /usr/bin/ruby
#

引数

#
p ARGV
puts "1個目の引数 = #{ARGV[0]}"
puts "2個目の引数 = #{ARGV[1]}"
puts "3個目の引数 = #{ARGV[2]}"
puts "引数の個数 = #{ARGV.size}"
["aaa", "bbb"]
1個目の引数 = aaa
2個目の引数 = bbb
3個目の引数 =
引数の個数 = 2

ワーク(文字列, 数値)

#
work = "abcdef"
puts "work = #{work}"
puts "work[2] = #{work[2]}"
puts "work[2].chr = #{work[2].chr}"
puts "work[2..3] = #{work[2..3]}"
puts "work[2,3] = #{work[2,3]}"
work = 12345
puts "work = #{work}"
puts "== work is #{work} =="
puts '== work is #{work} =='
work = abcdef
work[2] = 99
work[2].chr = c
work[2..3] = cd
work[2,3] = cde
work = 12345
== work is 12345 ==
== work is #{work} ==

配列

#
work = []
work = ["aaa", 12345, "ccc", 23456, "eee", 34567]
work2 = ["AAA", 54321, "ccc", 23456, "EEE", 76543]
p work
p work2
puts "work[0] = #{work[0]}"
puts "work[1] = #{work[1]}"
puts "work[2] = #{work[2]}"
puts "work[3] = #{work[3]}"
puts "work[4] = #{work[4]}"
puts "work[5] = #{work[5]}"
puts "work.size = #{work.size}"
puts "work[2..3] = #{work[2..3]}"
puts "work[2,3] = #{work[2,3]}"
puts "work & work2 = #{work & work2}"
puts "work | work2 = #{work | work2}"
puts "work + work2 = #{work + work2}"
puts "work - work2 = #{work - work2}"
["aaa", 12345, "ccc", 23456, "eee", 34567]
["AAA", 54321, "ccc", 23456, "EEE", 76543]
work[0] = aaa
work[1] = 12345
work[2] = ccc
work[3] = 23456
work[4] = eee
work[5] = 34567
work.size = 6
work[2..3] = ccc23456
work[2,3] = ccc23456eee
work & work2 = ccc23456
work | work2 = aaa12345ccc23456eee34567AAA54321EEE76543
work + work2 = aaa12345ccc23456eee34567AAA54321ccc23456EEE76543
work - work2 = aaa12345eee34567

ハッシュ

#
work = {}
work['apple'] = "red"
work['lemon'] = "yellow"
work['melon'] = "green"
p work
puts "work['apple'] = #{work['apple']}"
puts "work['lemon'] = #{work['lemon']}"
puts "work['melon'] = #{work['melon']}"

work2 = {"apple", "red", "lemon", "yellow", "melon", "green"}
p work2
puts "work2['apple'] = #{work2['apple']}"
puts "work2['lemon'] = #{work2['lemon']}"
puts "work2['melon'] = #{work2['melon']}"

work3 = {"apple" => "red", "lemon" => "yellow", "melon" => "green"}
p work3
puts "work3['apple'] = #{work3['apple']}"
puts "work3['lemon'] = #{work3['lemon']}"
puts "work3['melon'] = #{work3['melon']}"
{"apple"=>"red", "melon"=>"green", "lemon"=>"yellow"}
work['apple'] = red
work['lemon'] = yellow
work['melon'] = green
{"apple"=>"red", "melon"=>"green", "lemon"=>"yellow"}
work2['apple'] = red
work2['lemon'] = yellow
work2['melon'] = green
{"apple"=>"red", "melon"=>"green", "lemon"=>"yellow"}
work3['apple'] = red
work3['lemon'] = yellow
work3['melon'] = green

if文

#
work = {"apple" => "red", "lemon" => "yellow", "melon" => "green"}
#if (work['lemon'] == "red")
#if (work['lemon'] != "red")
#if (work['lemon'] > "red")
#if (work['lemon'] <"red")
#if (work['lemon'] >= "red")
#if (work['lemon'] <= "red")
if (work['lemon'] == "red")
       puts "lemon is red"
elsif (work['lemon'] == "yellow")
       puts "lemon is yellow"
else
       puts "lemon is not red and yellow"
end
lemon is yellow

if文(パターンマッチング)

#
work = {"apple" => "red", "lemon" => "yellow", "melon" => "green"}
if (/^y.*w$/ =~ work['lemon'])
       puts "lemon is y.*w"
else
       puts "lemon is not y.*w"
end
lemon is y.*w

case文

#
work = {"apple" => "red", "lemon" => "yellow", "melon" => "green"}
case work['lemon']
when "red"
       puts "lemon is red"
when "yellow"
       puts "lemon is yellow"
else
       puts "lemon is not red and yellow"
end
lemon is yellow

while文

#
work = ["melon", "lemon", "apple"]
i = 0
while (i < 3)
       if (work[i] == "apple")
#              next
#              redo
               break
       end
       puts work[i]
       i += 1
end
melon
lemon

for文

#
work = ["melon", "lemon", "apple"]
#for i in work
for i in work.sort
       puts i
end
apple
lemon
melon

for文(2)

#
work = ["melon", "lemon", "apple"]
for i in 0..2
       puts work[i]
end
melon
lemon
apple

each文

#
work = ["melon", "lemon", "apple"]
work.each do |i|
       puts i
end
melon
lemon
apple

for文(ハッシュの添字でループ)

#
work = {"melon", "green", "lemon", "yellow", "apple", "red"}
#for i in work.keys
for i in work.keys.sort
       puts work[i]
end
red
yellow
green

each文(ハッシュ)

#
work = {"melon" => "green", "lemon" => "yellow", "apple" => "red"}
work.each do |key, val|
       puts key + "=" + val
end
apple=red
lemon=yellow
melon=green

tr文(文字置換)

#
work = "ABCDEFG"
puts "work = #{work}"
puts "work.tr('C-E', 'c-e') = #{work.tr('C-E', 'c-e')}"
work = ABCDEFG
work.tr('C-E', 'c-e') = ABcdeFG

gsub文(文字列置換)

#
work = "ABCDEFG"
puts "work = #{work}"
puts "work.gsub(/C.*E/, 'cde') = #{work.gsub(/C.*E/, 'cde')}"
work = ABCDEFG
work.gsub(/C.*E/, 'cde') = ABcdeFG

gsub文(文字列置換)(2)

#
work = "ABCDEFG"
puts "work = #{work}"
puts "work.gsub(/(C.*E)/){'==' +  $1 + '=='} = #{work.gsub(/(C.*E)/){'==' +  $1 + '=='}}"
work = ABCDEFG
work.gsub(/(C.*E)/){'==' +  $1 + '=='} = AB==CDE==FG

gsub文(文字列置換)(3)

#
work = "%41%42%43+%44%45%46"
puts "work(URL encoded) = #{work}"
work = work.tr("+", " ")
work = work.gsub(/%([0-9a-zA-Z][0-9a-zA-Z])/){$1.hex.chr}
puts "work(URL decoded) = #{work}"
work(URL encoded) = %41%42%43+%44%45%46
work(URL decoded) = ABC DEF

gsub文に関して追記 2009.04.07

(a, b) = "aaa=".split("=")
b.gsub("bbb", "")

上記を実行すると、

./test.rb:?: private method `gsub' called for nil:NilClass (NoMethodError)

のようなエラーが発生する。bが空文字列なのがいけないらしい。これを回避するには、

(a, b) = "aaa=".split("=")
b.to_s.gsub("bbb", "")

の様に明示的にbを文字列クラスに変換してやるとよい。

split文(文字列分割)

#
work = "apple-=-lemon-=-melon-=-orange"
puts "work = #{work}"
work2 = work.split('-=-')
puts "work2 = work.split('-=-')"
puts "work2[0] = #{work2[0]}"
puts "work2[1] = #{work2[1]}"
puts "work2[2] = #{work2[2]}"
puts "work2[3] = #{work2[3]}"
work = apple-=-lemon-=-melon-=-orange
work2 = work.split('-=-')
work2[0] = apple
work2[1] = lemon
work2[2] = melon
work2[3] = orange

split文(文字列分割:分割制限有り)

#
work = "apple-=-lemon-=-melon-=-orange"
puts "work = #{work}"
(var1, var2, var3)  = work.split("-=-", 3)
puts "(var1, var2, var3)  = work.split('-=-', 3)"
puts "var1 = #{var1}"
puts "var2 = #{var2}"
puts "var3 = #{var3}"
work = apple-=-lemon-=-melon-=-orange
(var1, var2, var3)  = work.split('-=-', 3)
var1 = apple
var2 = lemon
var3 = melon-=-orange

length, index, rindex文(文字列長、位置取得)

#
work = "apple apple apple"
puts "work = #{work}"
puts "work.length = #{work.length}"
puts "work.index('apple') = #{work.index('apple')}"
puts "work.rindex('apple') = #{work.rindex('apple')}"
work = apple apple apple
work.length = 17
work.index('apple') = 0
work.rindex('apple') = 12

to_?文(整数化、実数化、文字列化)

#
works = "123"
puts "works = #{works}(is string)"
puts "works.to_i * 10 = #{works.to_i * 10}"
puts "works.to_f * 10 = #{works.to_f * 10}"
puts "works.to_f.to_s + works.to_f.to_s = #{works.to_f.to_s + works.to_f.to_s}"
works = 123(is string)
works.to_i * 10 = 1230
works.to_f * 10 = 1230.0
works.to_f.to_s + works.to_f.to_s = 123.0123.0

ファイル(書き込み:puts)

#
f = open("junk.txt", "w")
f.puts("line1")
f.puts("line2\n")
f.puts("line3\n\n")
f.close

ファイル(書き込み:write)

#
f = open("junk2.txt", "w")
f.write("line1")
f.write("line2\n")
f.write("line3\n\n")
f.close

ファイル(行読み込み:gets from puts)

#
f = open("junk.txt", "r")
while (r = f.gets)
       p r
       p r.chomp
       r.chomp!
       p r
end
f.close
"line1\n"
"line1"
"line1"
"line2\n"
"line2"
"line2"
"line3\n"
"line3"
"line3"
"\n"
""
""

ファイル(行読み込み:gets from write)

#

f = open("junk2.txt", "r")
while (r = f.gets)
       p r
       p r.chomp
       r.chomp!
       p r
end
f.close
"line1line2\n"
"line1line2"
"line1line2"
"line3\n"
"line3"
"line3"
"\n"
""
""

ファイル(ランダム読み:read)

#
f = open("junk.txt", "r")
#f.seek(6)
f.pos = 6
r = f.read(12)
p r
f.close
"line2\nline3\n"

ファイル(書き込み:putc)

#
work = "line1\nline2\nline3\n"
f = open("junk.txt", "w")
for i in 0..(work.length-1)
       f.putc(work[i])
end
f.close

ファイル(行読み込み:getc from putc)

#
f = open("junk.txt", "r")
while (ch = f.getc)
       print " #{ch.chr}"
end
f.close
 l i n e 1
 l i n e 2
 l i n e 3

【追記】標準入力、標準出力、エラー出力

標準入力、標準出力、エラー出力に対しては、それぞれSTDIN、STDOUT、STDERRが自動的にオープンされているので、いきなり

STDOUT.puts("ほげほげ")

とか、

r = STDIN.gets

みたいに書ける。

ファイル(外部コマンド実行)

#
f = open("|ls -al|head -5", "r")
while (r = f.gets)
       puts "==" + r.chomp + "=="
end
f.close
==合計 40==
==drwxr-xr-x 2 m-ito m-ito  4096 2009-03-13 19:45 .==
==drwxr-xr-x 3 m-ito m-ito  4096 2009-03-13 18:24 ..==
==-rw-r--r-- 1 m-ito m-ito  2896 2009-03-13 19:45 junk==
==-rw-r--r-- 1 m-ito m-ito    18 2009-03-13 19:45 junk.txt==

外部コマンド実行

#
for i in `ls -al|head -5`
       puts "==" + i.chomp + "=="
end
==合計 40==
==drwxr-xr-x 2 m-ito m-ito  4096 2009-03-13 19:45 .==
==drwxr-xr-x 3 m-ito m-ito  4096 2009-03-13 18:24 ..==
==-rw-r--r-- 1 m-ito m-ito  2896 2009-03-13 19:45 junk==
==-rw-r--r-- 1 m-ito m-ito    18 2009-03-13 19:45 junk.txt==

追記(2011/07/04) : ruby-1.9.xだと(Stringクラスからeachメソッドが削除されたので?)上記の書き方はできない。以下のようにする。

#
`ls -al|head -5`.each_line do |i|
       puts "==" + i.chomp + "=="
end

popen文(外部コマンド実行)

#! /usr/bin/ruby
io = IO.popen("cat -n", "r+")
io.puts("line1")
io.puts("line2")
io.puts("line3")
##io.flush      # flushをするのが確実。
io.close_write  # ただし、起動プログラムの作りによるとclose_writeする必要がある。
r = io.gets
puts r
r = io.gets
puts r
r = io.gets
puts r
io.close
     1  line1
     2  line2
     3  line3

system文(外部コマンド実行)

#
system("ls -al|head -5")
合計 40
drwxr-xr-x 2 m-ito m-ito  4096 2009-03-13 19:45 .
drwxr-xr-x 3 m-ito m-ito  4096 2009-03-13 18:24 ..
-rw-r--r-- 1 m-ito m-ito  3657 2009-03-13 19:45 junk
-rw-r--r-- 1 m-ito m-ito    18 2009-03-13 19:45 junk.txt

print文各種

#
work = "ABCDEFG"
p work
puts work
print work + "\n"
printf("%s %d %f\n", work, 123, 123.456)
"ABCDEFG"
ABCDEFG
ABCDEFG
ABCDEFG 123 123.456000

例外処理

全てのエラーをトラップする場合
begin
    エラーを検出したい処理
rescue => e
    エラー検出時のトラップ処理
    put e
else
    エラー未検出時の処理
ensure
    エラーの検出のいかんにかかわらず行う処理
end
特定のエラーをトラップする場合
begin
    エラーを検出したい処理
rescue Errno::EEXIST => e
    エラー検出時のトラップ処理
    put e
else
    エラー未検出時の処理
ensure
    エラーの検出のいかんにかかわらず行う処理
end

「=>e」は省略可能。

ファイル操作

#
File.umask(022)
File.chmod(0777, "junk.txt")
File.rename("junk.txt", "junk3.txt")
File.unlink("junk2.txt")
File.unlink("junk3.txt")

mkdir, rmdir(ディレクトリ操作)

#
retry_max = 3
retry_cnt = 0
begin
       Dir.mkdir("lock.dir", 0755)
rescue Errno::EEXIST
       puts "lock NG"
       retry_cnt += 1
       if (retry_cnt < retry_max)
               retry
       else
               puts "lock give up!"
       end
else
       puts "lock OK"
ensure
       Dir.rmdir("lock.dir")
end
lock OK

関数定義

#
def myfunc(v1="defval1", v2="defval2")
       local_work = "initialized in myfunc"

       puts "$Global_work = #{$Global_work}"
       puts v1
       puts v2
       return "ok"
end

$Global_work = "initialized in main"
local_work = "initialized in main"

puts myfunc()
puts myfunc("poipoi", "hogehoge")

puts "local_work = #{local_work}"
$Global_work = initialized in main
defval1
defval2
ok
$Global_work = initialized in main
poipoi
hogehoge
ok
local_work = initialized in main

クラス定義

#
class Enzan
       attr_accessor :x, :y
       attr_reader :x_readonly, :y_readonly
       attr_writer :x_writeonly, :y_writeonly

       def initialize(i=100, j=200)
               @x = i
               @y = j
       end

       def tasu
               @@global_in_class_work = @x + @y
               return @x + @y
       end

       def hiku
               @@global_in_class_work = @x - @y
               return @x - @y
       end

       def compute(i)
               case i
               when "+"
                       @@global_in_class_work = @x + @y
                       return @x + @y
               when "-"
                       @@global_in_class_work = @x - @y
                       return @x - @y
               else
                       return 0
               end
       end

       def global_in_class_work
               return @@global_in_class_work
       end
end

e = Enzan.new
puts e.tasu
puts e.hiku
puts e.compute("+")

f = Enzan.new(10, 20)
puts f.tasu
puts f.hiku
puts f.compute("+")

f.x = 1000.0
f.y = 2000.0
puts f.tasu
puts f.hiku
puts f.compute("-")

puts e.global_in_class_work
300
-100
300
30
-10
30
3000.0
-1000.0
-1000.0
-1000.0

クラス定義(継承有り)

#
class Oya
       def msg1(arg = "Oya.msg1")
               puts arg + " from Oya.msg1"
       end
       def msg2(arg = "Oya.msg2")
               puts arg + " from Oya.msg2"
       end
end

class Kodomo < Oya
       def msg2(arg = "Kodomo.msg2")
               super(arg)
               puts arg + " from Kodomo.msg2"
       end
       def msg3(arg = "Kodomo.msg3")
               puts arg + " from Kodomo.msg3"
       end
end

msg = Kodomo.new
msg.msg1("message 1")
msg.msg2("message 2")
msg.msg3("message 3")
message 1 from Oya.msg1
message 2 from Oya.msg2
message 2 from Kodomo.msg2
message 3 from Kodomo.msg3

環境変数取得

#
puts ENV['PATH']
/home/m-ito/bin:/home/m-ito/etc:/home/m-ito/prov/bin:/usr/local/bin:/usr/local/etc:/usr/local/sbin:/usr/bin:/usr/etc:/bin:/etc:/usr/sbin:/sbin:/usr/X11R6/bin:/opt/gnome/bin:/opt/kde/bin:/usr/local/jvim2.0/bin:/usr/local/canna/bin:/usr/local/lib/w3m:/usr/local/ssl/bin:/usr/local/mrtg/bin:/usr/local/Adobe/Reader8/bin

日時時刻取得

#
day = Time.now
puts day
puts day.year
puts day.month
puts day.day
puts day.hour
puts day.min
puts day.sec
puts day.wday
Fri Mar 13 19:45:44 +0900 2009
2009
3
13
19
45
44
5

kill文(シグナル送信)

#
begin
Process.kill("TERM", 1234)
rescue Errno::ESRCH
       puts "no such process"
else
       puts "signal send ok"
ensure
end

my_pid = $$
begin
Process.kill(0, my_pid)
rescue Errno::ESRCH
       puts "pid(#{my_pid}) is dead"
else
       puts "pid(#{my_pid}) is alive"
ensure
end
no such process
pid(3544) is alive

nkf文(文字コード変換)

#
require "nkf"
puts NKF.nkf("-S -X -e", "\x8a\xbf\x8e\x9a\x82\xc5\x82\xb7")
漢字です

timeout文

#
require "timeout"
begin
       timeout(3){
               sleep 10
       }
rescue Timeout::Error
       puts "timeout happened!"
else
       puts "10sec sleepd"
ensure
end
timeout happened!

yield(イテレータ作成)

#
def hoge(arg1, arg2)
       puts "arg1=#{arg1}, arg2=#{arg2}"
       yield(arg1, arg2, arg1 * arg2)
end

hoge(12,13) do |v1, v2, v3|
       puts "v1=#{v1}, v2=#{v2}, v3=#{v3}"
end
arg1=12, arg2=13
v1=12, v2=13, v3=156

_ ruby(1.8.x)儂(わし)的解釈によるメモ(クライアント編)

#! /usr/bin/ruby
require "socket"

s = TCPSocket.new("localhost", 12345)
s.sync = true
s.puts "#{ARGV[0]}"
print s.gets
s.puts "Q"

_ ruby(1.8.x)儂(わし)的解釈によるメモ(サーバ編)

#! /usr/bin/ruby
require 'socket'

def daemon
#
# バックグラウンドに移行する
# プロセスグループリーダ(、セッションリーダ)でなくなる -> 次のsetsidが成功する条件
# 制御端末はまだ持っている
       if (pid = fork)
               exit 0
       end
#
# 新しいセッションのリーダとなり制御端末を持たなくなる
# 新しいプロセスグループのリーダとなる
       Process.setsid
#
# セッションリーダ、プロセスグループリーダでなくなる
# よって、再び制御端末を持つ事ができなくなる
       if (pid = fork)
               exit 0
       end
#
       Dir.chdir("/")
       File.umask(0000)
       STDIN.close
       STDOUT.close
       STDERR.close

       Signal.trap("SIGTERM"){
               exit 0
       }
       Signal.trap("SIGHUP"){
               exit 0
       }
       Signal.trap("SIGINT"){
               exit 0
       }
end

daemon

port = 12345

gs = TCPServer.open(port)
while true
  ns = gs.accept
#  p ns.peeraddr
  Thread.start(ns) do |s|
    s.sync = true
    while l = s.gets
      break if /^q/i =~ l
      l.chomp!
      s.puts "#{l.swapcase}"
    end
    s.close
  end
end

_ ruby(1.8.x)儂(わし)的解釈によるメモ(基本編の追加編)

times文

3.times do |i|
	puts i
end
0
1
2

step文

1.step(10, 3) do |i|
	puts i
end
1
4
7
10

loop文

i = 0
loop do
	puts i
	i += 1
	if (i > 3)
		break
	end
end
0
1
2
3

module文

module Mymodule
	def msg(i)
		puts "#{i} is hogehoge"
	end
	def msg2(i)
		puts "#{i} is poipoi"
	end
	module_function :msg, :msg2
end

class Myclass
	include Mymodule
	def initialize(i)
		@msg = i
	end
	def hoge
		msg(@msg)
	end
	def poi
		msg2(@msg)
	end
end

mc = Myclass.new("hogepoi")
mc.hoge
mc.poi
hogepoi is hogehoge
hogepoi is poipoi

演算式(余り:%, べき乗:**)

puts 12 % 5
puts 2 ** 8
2
256

ビット演算式

a = 0
printf("%d\n", ~a)

a = 0x77
b = 0xee
printf("%x\n", a & b)

a = 0x77
b = 0xee
printf("%x\n", a | b)

a = 0x77
b = 0xee
printf("%x\n", a ^ b)

a = 0x77
printf("%x\n", a << 1)

a = 0xee
printf("%x\n", a >> 1)
-1
66
ff
99
ee
77

条件文(結合)

a = 10
b = 20
if (a < 15 && b > 15)
	puts "true"
else
	puts "false"
end

if (a < 15 || b < 15)
	puts "true"
else
	puts "false"
end
true
true

配列操作

a = ["w", "x", "y", "z"]
a[1, 2] = ["a", "b"]
p a
["w", "a", "b", "z"]
a = ["w", "x", "y", "z"]
a[1, 0] = ["a", "b"]
p a
["w", "a", "b", "x", "y", "z"]
a = ["w", "x", "y", "z"]
a.push("a")
p a
["w", "x", "y", "z", "a"]
p a.pop
p a
"a"
["w", "x", "y", "z"]
p a.shift
p a
"w"
["x", "y", "z"]
a = ["a", "x", "b", "x", "c"]
a.delete("x")
p a
["a", "b", "c"]
a = ["a", "x", "b", "x", "c"]
a.delete_at(2)
p a
["a", "x", "x", "c"]
a = ["a", "x", "b", "x", "c"]
p a.uniq
["a", "x", "b", "c"]
p "aaa" <=> "bbb"
p "bbb" <=> "aaa"
p "aaa" <=> "aaa"

p a.sort do |a, b|
	a <=> b
end
-1
1
0
["a", "b", "c", "x", "x"]

文字列の囲み

s = "aaaaa"
puts %Q{" ' #{s} ' "}
puts %q{" ' #{s} ' "}
" ' aaaaa ' "
" ' #{s} ' "

ヒアドキュメント

s = <<EOD
this is line1
  this is line2
    this is line3
EOD
puts s
this is line1
  this is line2
    this is line3

文字列操作

a = "   ABcdefgabcdeFG   "
puts a.delete("cde")
puts a.strip
puts a.upcase
puts a.downcase
puts a.swapcase
   ABfgabFG
ABcdefgabcdeFG
   ABCDEFGABCDEFG
   abcdefgabcdefg
   abCDEFGABCDEfg

ハッシュ操作

h = {"apple"=>"red", "lemon"=>"yellow", "melon"=>"green"}
p h.has_key?("lemon")
p h.has_value?("yellow")
p h.empty?
true
true
false

ハッシュ初期化

h = {"apple"=>"red", "lemon"=>"yellow", "melon"=>"green"}
i = h
h.clear
p h
p i
{}
{}
h = {"apple"=>"red", "lemon"=>"yellow", "melon"=>"green"}
i = h
h = {}
p h
p i
{}
{"melon"=>"green", "apple"=>"red", "lemon"=>"yellow"}

正規表現

a = "abcde\nfghijk\nlmnop"
p /^f.*k$/ =~ a
p /\Af.*k\Z/ =~ a
p /^a.*p$/ =~ a
p /\Aa.*p\Z/m =~ a
6
nil
nil
0

繰り返し(文字列scan)

a = "axxxb...ayyyb...azzzb"
a.scan(/a...b/) do |i|
	puts i
end
axxxb
ayyyb
azzzb

同期出力

STDOUT.puts "aaa"
STDOUT.flush

STDOUT.sync = true
STDOUT.puts "bbb"
aaa
bbb

ディレクトリ操作

puts cd = Dir.pwd
Dir.chdir("/tmp")
puts Dir.pwd
Dir.chdir(cd)
puts Dir.pwd
/home/zaurus/tmp
/tmp
/home/zaurus/tmp

ディレクトリ操作(2)

dir = Dir.open("/")
while (name = dir.read)
	puts name
end
dir.close
.
..
bin
dev
etc
lib
mnt
opt
tmp
var
usr
boot
home
proc
sbin
root

ファイル属性取得

printf("%o\n", File.stat("/etc/fstab").mode)
puts File.stat("/etc/fstab").uid
puts File.stat("/etc/fstab").gid
puts File.stat("/etc/fstab").size
puts File.stat("/etc/fstab").atime
puts File.stat("/etc/fstab").mtime
puts File.stat("/etc/fstab").ctime
100644
0
0
222
Sun Nov 11 23:30:54 +0900 2007
Sun Nov 11 23:30:54 +0900 2007
Sun Nov 11 23:30:54 +0900 2007

ファイル属性変更

f = open("junk.txt", "w")
f.close
puts atime = File.stat("junk.txt").atime
puts mtime = File.stat("junk.txt").mtime
File.utime(atime + 60, mtime + 60, "junk.txt")
puts atime = File.stat("junk.txt").atime
puts mtime = File.stat("junk.txt").mtime
File.unlink("junk.txt")
Sat Mar 14 18:53:35 +0900 2009
Sat Mar 14 18:53:35 +0900 2009
Sat Mar 14 18:54:35 +0900 2009
Sat Mar 14 18:54:35 +0900 2009

ファイル属性変更(2)

#f = open("junk.txt", "w")
#f.close
#File.chown(0, 0, "junk.txt")
#File.unlink("junk.txt")

ファイルテスト

puts File.exist?("/etc/fstab")
puts File.file?("/etc/fstab")
puts File.directory?("/etc/fstab")
puts File.readable?("/etc/fstab")
puts File.writable?("/etc/fstab")
puts File.executable?("/etc/fstab")
puts File.zero?("/etc/fstab")
puts File.size("/etc/fstab")
true
true
false
true
false
false
false
222

ファイル名操作

puts File.basename("/usr/local/doc/ruby.doc", ".doc")
puts File.dirname("/usr/local/doc/ruby.doc")
puts File.expand_path("./junk.txt")
ruby
/usr/local/doc
/home/root/usr/local/home/zaurus/tmp/junk.txt

eval文

#! /usr/bin/ruby

def func_print(str)
        print str
end

eval_str = ARGV[0] + '("hello world\n")'
eval eval_str
$ ./evaltest.rb func_print
hello world

取り扱いに注意しないと、いろんな物を注入(Injection)されちゃう。

_ ruby(1.8.x)儂(わし)的解釈によるメモ(CGI編)

CGIサンプル(ソースは以下を参照)

#! /usr/bin/ruby
#
# 産まれて初めてのrubyによるCGIプログラム...
#
require "cgi"
require "nkf"

cgi = CGI.new("html3")

in1 = NKF.nkf("-X -e", cgi['in1'])

cgi.out({"type" => "text/html",
               "charset" => "euc-jp"}){
       cgi.html("PRETTY" => true){
               cgi.head(){
                       cgi.title(){"rubytest1"}
               }
               cgi.body(){
                       cgi.div({"align" => "center"}){
                               "rubytest1"
                       } +
                       CGI.escapeHTML("Input is #{in1}") +
                       cgi.br() +
                       cgi.form({"action" => "rubytest1.cgi",
                                       "method" => "post"}){
                               cgi.input({"type" => "text",
                                               "name" => "in1",
                                               "id" => "in1"}) +
                               cgi.input({"type" => "submit",
                                               "value" => "ok"})
                       }
               }
       }
}
exit 0

_ ruby(1.8.x)儂(わし)的解釈によるメモ(未検証)

pack, unpack

arry = [0x41, 0x42, 0x43] # A,B,C
buf = arry.pack("C3")
p buf
buf = sock.read(4)
arry = buf.unpack("N") # Network byte order Integer(BIG ENDIAN)
p arry[0]