Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kakikubo committed Dec 25, 2010
0 parents commit d3aa32b
Show file tree
Hide file tree
Showing 149 changed files with 2,717 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 9.2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def foo
open("/no/file")
end

def bar
foo()
end

begin
bar()
rescue => ex
print ex.message, "\n"
end



5 changes: 5 additions & 0 deletions Dir.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dir.open("/usr/bin"){ |dir|
dir.each{ |name|
p name
}
}
1 change: 1 addition & 0 deletions FILE_LINE.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("file #{__FILE__}: #{__LINE__}")
12 changes: 12 additions & 0 deletions M.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module M
def meth
puts "meth"
end
end

class C
include M
end

c = C.new
c.meth
31 changes: 31 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rake/testtask'
lib_dir = File.expand_path('lib')
test_dir = File.expand_path('test')

# Rakefile
desc "Cross the bridge."
task :cross_bridge => [:build_bridge] do
puts "I'm crossing the bridge."
end

desc "Build the bridge."
task :build_bridge do
puts 'Bridge construction is complete.'
end

task :default => [:cross_bridge]

Rake::TestTask.new('test-file') do |t|
t.test_files = ['test/tc_datafile.rb',
'test/tc_datafilewriter.rb',
'test/tc_datafilereader.rb']
t.warning = true
end

Rake::TestTask.new('test-console') do |t|
t.test_files = ['test/tc_console.rb',
'test/tc_prettyprinter.rb']
t.warning = true
end

task 'test' => ['test-file', 'test-console']
16 changes: 16 additions & 0 deletions acc_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AccTest
def pub
puts "pub is a public method"
end

private
def priv
puts "priv is a private method"
end


end

acc = AccTest.new
acc.pub
acc.priv
16 changes: 16 additions & 0 deletions acc_test_flymake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AccTest
def pub
puts "pub is a public method"
end

private
def priv
puts "priv is a private method"
end


end

acc = AccTest.new
acc.pub
acc.priv
6 changes: 6 additions & 0 deletions accesslog_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 全体のアクセス数を調べる
count = 0
while line = gets()
count += 1
end
print count,"\n"
4 changes: 4 additions & 0 deletions accesslog_count2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 全体のアクセス数を調べる(readlines版)
#lines = readlines
#print lines.length,"\n"
print readlines.length,"\n"
8 changes: 8 additions & 0 deletions accesslog_date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 日ごとのアクセス数を調べる・日付の出力
while line = gets()
datetime = line.chomp.split(/\s/)[3]
if %r|\[(\d+)/(\w+)/(\d+)| =~ datetime
day, month, year = $1, $2, $3
print "#{year}/#{month}/#{day}\n"
end
end
12 changes: 12 additions & 0 deletions accesslog_date2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 日ごとのアクセス数を調べる・日付の正規化
MONTH = {'jan' => 1, 'feb' => , 'mar' => 3, 'apr' => 4,
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12}

while line = gets()
datetime = line.chomp.split(/\s/)[3]
if %r|\[(\d+)/(\w+)/(\d+)| =~ datetime
day, month, year = $1, MONTH[$2.downcase], $3
print "#{year}/#{month}/#{day}\n"
end
end
19 changes: 19 additions & 0 deletions accesslog_date3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 日ごとのアクセス数を調べる・日付の正規化
MONTH = {'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12}

count = Hash.new(0)

while line = gets()
datetime = line.chomp.split(/\s/)[3]
if %r|\[(\d+)/(\w+)/(\d+)| =~ datetime
day, month, year = $1, MONTH[$2.downcase], $3
count["#{year}/#{month}/#{day}"] += 1
end
end


count.sort{ |a,b| a[0] <=> b[0] }.each{ |key,value|
print key, ":", value, "\n"
}
20 changes: 20 additions & 0 deletions accesslog_date4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 日ごとのアクセス数を調べる・日付の正規化
MONTH = {'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12}

count = Hash.new(0)
ratio = 0.1

while line = gets()
datetime = line.chomp.split(/\s/)[3]
if %r|\[(\d+)/(\w+)/(\d+)| =~ datetime
day, month, year = $1, MONTH[$2.downcase], $3
count["#{year}/#{month}/#{day}"] += 1
end
end


count.sort{ |a,b| a[0] <=> b[0] }.each{ |key,value|
printf("%10s %5d: %-60s \n",key,value,"#"*(value*ratio))
}
39 changes: 39 additions & 0 deletions accesslog_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 日ごとのアクセス数を調べる・アクセス数の出力
#FIXME
MONTH = {'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12}

pattern = ARGV.shift
count = Hash.new(0)

while line = gets()
column = line.chomp.split
path = column[6]
#next if path !~ pattern #ここは書籍とおりにするとエラーになる。
=begin
上記のエラー内容は以下の通り。
Kacintosh% ruby accesslog_file.rb / /opt/local/apache2/logs/access_log
accesslog_file.rb:13:in `=~': type mismatch: String given (TypeError)
From accesslog_file.rb:13
なんで…?
=end
next if path != "#{pattern}"
=begin
これなら大丈夫。正規表現なのがそもそも間違い?
いや、でもパターンを引数に取るのだからいいわけか。
じゃあ、正規表現で本当にパターンマッチさせるなら…?
next if path !~ /#{pattern}/
こうなるね。なんだ。これだけか。
=end
datetime = column[3]
if %r|\[(\d+)/(\w+)/(\d+)| =~ datetime
day, month, year = $1, MONTH[$2.downcase], $3
count["#{year}/#{month}/#{day}"] += 1
end
end

count.sort{ |a,b| a[0] <=> b[0] }.each{ |key,value|
print key, ":", value, "\n"
}
5 changes: 5 additions & 0 deletions ad2heisei.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 西暦から平成に変換する

ad = ARGV[0].to_i
heisei = ad - 1988
print heisei,"\n"
5 changes: 5 additions & 0 deletions ad2heisei_flymake.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 西暦から平成に変換する

ad = ARGV[0].to_i
heisei = ad - 1988
print heisei,"\n"
11 changes: 11 additions & 0 deletions alias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class String
alias buffer_size length
def length
self.split(//).length
end
end

j_string = "日本語の文字列"
p j_string.buffer_size
p j_string.length

7 changes: 7 additions & 0 deletions arg_arith.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
num0 = ARGV[0].to_i
num1 = ARGV[1].to_i

print num0, " + ",num1, " = ", num0 + num1, "\n"
print num0, " - ",num1, " = ", num0 - num1, "\n"
print num0, " * ",num1, " = ", num0 * num1, "\n"
print num0, " / ",num1, " = ", num0 / num1, "\n"
6 changes: 6 additions & 0 deletions backquote_sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dirlist = `ls`
dirlist.each do |line|
if line =~ /.rb$/i
print line
end
end
14 changes: 14 additions & 0 deletions bit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def pb(i)
# printfの%bフォーマットを使って
# 整数の末尾8ビットを2進数表示する
printf("%08b\n", i & 0b11111111)
end

b = 0b11110000
pb(b)
pb(~b)
pb(b & 0b00010001)
pb(b | 0b00010001)
pb(b ^ 0b00010001)
pb(b >> 3)
pb(b << 3)
9 changes: 9 additions & 0 deletions book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Book
attr_accessor :title, :author, :genre
def initialize(title,author,genre=nil)
@title = title
@author = author
@genre = genre
end
end

18 changes: 18 additions & 0 deletions book_cmp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Book
include Comparable

def <=>(other)
t = @genre.to_s <=> other.genre.to_s #ジャンルを比較
return t if t != 0
return @title <=> other.title
end

attr_accessor :title, :author, :genre

def initialize(title,author,genre=nil)
@title = title
@author = author
@genre = genre
end
end

12 changes: 12 additions & 0 deletions book_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'book_cmp'

ary = []
ary << Book.new("Software", "Rucker", "SF")
ary << Book.new("BABEL-17", "Delany", "SF")
ary << Book.new("Programming Perl", "Wall", "Computer")
ary << Book.new("Programming Pearls", "Bentley", "Computer")

ary.sort.each{ |book|
printf "%-10s %-20s %s\n", book.genre, book.title, book.author
}

72 changes: 72 additions & 0 deletions booklist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'book'

class BookList
## 初期化
def initialize()
@booklist = Array.new
end

## 新しい本を加える
def add(book)
@booklist.push(book)
end

## 本の冊数を返す
def length
@booklist.length
end

## n番目に格納されている本を別の本にする
def []=(n,book)
@booklist[n] = book
end

## n番目に格納されている本を返す
def [](n)
@booklist[n]
end

## 本をリストから削除する
def delete(book)
@booklist.delete(book)
end

## イテレータを定義
def each
@booklist.each{ |book|
yield(book)
}
end

## (タイトルだけ取ってくる)イテレータを定義
def each_title
@booklist.each{ |book|
yield(book.title)
}
end
## タイトルと著者名を返すイテレータを定義
def each_title_author
@booklist.each{ |book|
yield(book.title, book.author)
}
end

def find_by_author(author)
if block_given?
@booklist.each{ |book|
if author =~ book.author
yield(book)
end
}
else ## ブロックが無い場合
result = []
@booklist.each{ |book|
if author =~ book.author
result << book
end
}
end
end


end
10 changes: 10 additions & 0 deletions booklist_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "booklist"

class BookList
include Enumerable
def each
@booklist.each{ |book|
yield(book)
}
end
end
Loading

0 comments on commit d3aa32b

Please sign in to comment.