Author: satoshiokita
DATE:2006/02/13 TIME:04:23:45 JST
fliename: 01_back_quote.rb
#!/usr/local/bin/ruby
#
# Built-in Functions ( that was defined methods in Kernel Module )
#
# ` function
#
# `string` function execute external commands.
#
`echo "ehllo external command!\n"`
print "return code is " , $? , "\n"
fliename: 02_Array_function.rb
#!/usr/local/bin/ruby
#
# Built-in Functions ( that was defined methods in Kernel Module )
#
# Array(arg) function
#
#
testData= {
"Java" =>gt; "James Gosling",
"Ruby" =>gt; "Matz"
}
aryObject = Array(testData);
# reflection
p aryObject.class
p aryObject.methods
#
print aryObject.size
print aryObject[0] , "\n"
print aryObject[1] , "\n"
fliename: 03_Float_function.rb
#!/usr/local/bin/ruby
#
# Built-in Functions ( that was defined methods in Kernel Module )
#
begin
print "begin program."
#
# Float(arg) function
#
p Float ("10.0")
p Float ("10")
p Float ("1e-3")
p Float (".123")
p Float ("")
rescue ArgumentError =>gt; evar
printf "*** begin exception handling\n"
p $!
p evar
printf "*** end exception handling\n"
end
print "end program."
fliename: 040_grep.rb
#!/usr/local/bin/ruby
#
# REFFERENCE
# http://www.ruby-lang.org/ja/20020314.html
#
# ex). ruby 04_grep.rb AAA 04_grep.rb
#
$pat = ARGV.shift
while $_ = gets
# basic
if /#{$pat}/
print $_
end
if $_ =~ /^#FOO.*R$/
print $_
end
if $_ =~ /^#BAR.+0$/
print $_
end
# Wiki!!!
if $_ =~ /^#(([A-Z][a-z]+([A-Z][a-z]+)+))$/
print "***"
print $_
end
end
#FOOR
#FOOaaaR
#BAR0
#BARz0
# Wiki!!!
#WikiWiki
#YukiWiki
#OkiWiki
#OkiWikiMini
fliename: 041_grep.rb
#!/usr/local/bin/ruby
msg1="[comments]"
msg2="a[comments]"
msg3="a[comments]"
ary = "[comments]","a[comments]","[comments]a","a[comments]a",""
ary.each{ |i|
print "*****\n"
p i
rs = i.gsub(/^\[comments\]$/, "hoge")
p i, "===", rs
}
result = msg1.gsub(/^\[comments\]$/, "hoge")
p msg1
p result
print "***********************\n"
testmsg="[comments]"
testmsg2="a[comments]a"
if ( testmsg =~ /^\[comments\]$/ )
print "1:match\n"
end
if ( testmsg2 =~ /^\[comments\]$/ )
print "2:match\n"
end
fliename: 042_grep.rb
#!/usr/local/bin/ruby
def grep_test(src)
return src.grep(
/(^[0-9]*$)/
) { |buff|
buff = "#{$1}"
}
end
p grep_test("123")
p grep_test("ABC")
def grep_test2(src)
return src.grep(
/(^\[comments_[0-9]*\]$)/
) { |buff|
buff = "#{$1}"
}
end
p grep_test2("123")
p grep_test2("ABC")
p grep_test2("comments_1")
p grep_test2("comments_12")
p grep_test2("comments_12A")
p grep_test2("[comments_12]")
def grep_test3(src)
return src.grep(
/(^((\[)(comments_[0-9]*)(\]))$)/
) { |buff|
buff = "#{$1}"
}
end
print "***\n"
p grep_test3("[comments_12]")
p grep_test3("a[comments_12]b")
p grep_test3("[comments_12]b")
p grep_test3("a[comments_12]")
def grep_test4(src)
return src.grep(
/^((\[)(comments_[0-9]*)(\]))$/
) { |buff|
if ( buff =~ /^((\[)(comments_[0-9]*)(\]))$/)
buff = "#{$3}"
end
}
end
print "***\n"
p grep_test4("[comments_12]")
p grep_test4("a[comments_12]b")
p grep_test4("[comments_12]b")
p grep_test4("a[comments_12]")
fliename: 043_grep.rb
#!/usr/local/bin/ruby
test_data = "123"
if ( test_data =~ /^[1][2][3]$/ )
print "1:match\n"
end
if ( test_data =~ /^[1-9]*$/ )
print "2:match\n"
end
if ( test_data =~ /^([1-9]*?)$/ )
print "3:match\n"
end
test_data = "[comments_123]"
if (test_data =~ /^(\[)(comments_)([1-9]*?)(\])$/ )
print "#{$1}, #{$2}, #{$3}, #{$4}"
print "4:match\n"
end
fliename: 044_grep_multilines.rb
#!/usr/local/bin/ruby
#
# this's the Ruby multilines grep sample program.
# it's use the m option into clojor of grep method.
#
# これは、Rubyでの複数行正規表現のサンプルプログラム.
# grepメソッドのクロージャの中でmオプションをつかう.
#
# author:satoshiokita gt;
# date :$Date: 2006-02-09 01:22:30 +0900 (木, 09 2 2006) $
# Version:$Rev: 1340 $
#
$KCODE = "u"
testdata="001\nabc"
#p testdata
#print testdata
#p testdata.size
print "*** execute grep\n"
# クロージャにするとmオプションをつけてもマッチしない?!
testdata.grep(
/001.*?abc/m
) { |b|
print b
}
print "*** execute grep\n"
# =~演算子での正規表現の場合は、マッチする.
if ( testdata =~ /001.*?abc/m )
print "#{$0}\n"
end
print "*** .* grep\n"
print testdata
testdata.grep(
/001.*?abc/m
) { |b|
p b
}
testdata2="/* aaa
* bbb
* ccc
*/"
#print testdata2
if (testdata2 =~ /\/\*.*?\*\//m)
print "match multi\n"
end
fliename: 045_grep_domain_parse.rb
#!/usr/local/bin/ruby
"http://www.oklab.org/cgi-bin/OkiBlog.cgi#5".grep(
/((http:\/\/)(.*?)(\/.*))/
) { |b|
p "#{$1}"
p "#{$2}"
p "#{$3}"
p "#{$4}"
}
fliename: 051_file.rb
#!/usr/local/bin/ruby
#fp = File.open("test.txt", File::Constants::WRONLY | File::Constants::CREAT )
#fp = File.open("test.txt", "r+")
fp = File.open("test.txt", "a+")
# $B%U%!%$%k$N@hF,$+$i=q$-9~$a$k$+%F%9%H(B
p fp.tell
fp.pos = 0
p fp.tell
fp.seek(0, IO::SEEK_SET)
fp.rewind
p fp.tell
fp.puts("!!!0")
fp.flush
fp.close
# $B$G$-$J$$(B
fliename: 052_file.rb
#!/usr/local/bin/ruby
filename = "test.txt"
buff = "buffer***"
# rename
fp = File.open(filename, "r")
File.rename(filename, filename + ".wk")
fp.close
# rewrite
fp = File.open(filename, "w")
fp.puts(buff)
File.open(filename + ".wk", "r") { |wk_fp|
fp.puts(wk_fp.readlines)
}
# remove
File.unlink(filename + ".wk")
fp.close
fliename: BuiltIn_Constraints.rb
#!/usr/local/bin/ruby
#
# build-in constraints
# STDIN
# STDOUT
# STDERR
# ENV
ENV.each { |i|
print i , "\n"
}
# ARGF
print "*** ARGF\n"
p ARGF.file
p ARGF.filename
# ARGV ( alias $* )
p ARGV.size
# DATA
# TOPLEVEL_BINDING
# Ruby Language Infomation.
p RUBY_VERSION
p RUBY_RELEASE_DATE
p RUBY_PLATFORM
fliename: BuiltIn_Variables.rb
#!/usr/local/bin/ruby
#
# built-in variables
#
# Global Variable is String of beginning '$' mark.
# Built-in Variable is Ruby Language is defined Global Variables
# Specific Variable is specific in Built-in variables.
# Option Variables is defined following
# '$-' + one charactor.
#
fliename: argv.rb
#!/usr/local/bin/ruby
#
# argv
#
# ruby argv.rb aaa bbb ccc
#
# program name
print $0 , "\n"
# arguments
p ARGV.class
p ARGV.to_s
ARGV.each { |i|
p i , "\n"
}
p ARGV.size
p ARGV[0]
p ARGV[1]
p ARGV[2]
fliename: environment.rb
#!/usr/local/bin/ruby
#
# System Environment
#
# Ruby Environment Constraint
# * RUBYOPT - appending commant line option. read after default option.
# * RUBYLIB - extending search path
# * RUBYPATH - search path (default is $PATH)
# * RUBY_LIBPREFIX - Dynamic Load Module loding search path.
p ENV
p ENV.size
print " #{ENV["TERM"]}. \n"
p ENV["TERM"]
p ENV['TERM']
print " #{ENV["SHELL"]}. \n"
p ENV["SHELL"]
p ENV['SHELL']
fliename: fileoutput.rb
debug_out = File.open("ruby_debug.log", "w", 0666)
p debug_out.methods
if ($DEBUG)
p "hoge"
end
debug_out.puts "aaaa"
debug_out.close
fliename: print_ascii_concat.rb
#!/usr/local/bin/ruby
# print the ASCII characterset.
#
str = ""
str.concat(65) # decimal. NOT hex.
p str
str2 = ""
for cnt in 0..128
str2.concat(cnt)
end
p str2
fliename: split.rb
#!/usr/local/bin/ruby
str="HELLO\nWOLD"
aryObj = str.split(/\n+/)
for i in aryObj
print i , "\n"
end