-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d3aa32b
Showing
149 changed files
with
2,717 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Dir.open("/usr/bin"){ |dir| | ||
dir.each{ |name| | ||
p name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("file #{__FILE__}: #{__LINE__}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.