p Time.now.strftime("%Y/%m/%d:%H:%M:%S")
puts()
puts("999".to_i())
puts("99.9".to_f())
p "abCdefG".upcase()
p "abCdefG".downcase()
puts "abcdefg"[0..2]
puts "abcdefg".slice(0,3)
puts " aaabbb ".strip()
puts "Apple Banana Apple Orange".sub("Apple", "Pine")
puts "Apple Banana Apple Orange".gsub("Apple", "Pine")
puts "Apple Banana Apple Orange".index("Banana").to_s
puts "Apple Banana Apple Orange".rindex("Apple").to_s
p "ABCDEFG".include?("EFG")
p "ABCDEFG".include?("XYZ")
s = "HelloWorld\r\n"
p s
p s.chomp()
datas = "abc123abc456abc789".scan(/\d{3}/);
puts(datas);
puts("--------------------> 制御文")
puts("----> if");
if_a_v =2;
if if_a_v == 1
puts("if_a_v == 1")
elsif if_a_v == 2
puts("if_a_v == 2")
end
puts("----> case");
case if_a_v
when 1
puts("if_a_v==1");
when 2
puts("if_a_v==2");
end
puts("----> for");
for i in 5.times
puts(i);
end
puts("----> while");
count=0;
while count < 5
puts(count);
count+=1;
end
puts("----> Array.each");
datas = [11,22,33,44,55];
datas.each do |data|
puts(data);
end
puts("----> times");
5.times() do |i|
puts(i);
end
puts("--------------------> 基本")
puts("----> 文字列長(length)")
p "あいうえおavc".length()
puts "イテレータ"
(1..10).each do |i|
puts i.to_s
end
puts("----> 配列")
array = ["aaa","bbb","ccc"]
array.each do |a|
puts a
end
puts("----> each_with_index")
array.each_with_index do |item, index|
p [index, item]
end
puts("----> 結合")
puts array.join("+")
puts("----> 配列同士の結合")
pa1 = [1,2,3,4]
pa2 = [4,5,6]
p pa1.concat(pa2)
puts("----> 配列の和、積")
pa1 = [1,2,3,4]
p pa1 | pa2
p pa1 & pa2
puts("----> 末尾に追加")
str_arr = []
str_arr.push("data1");
str_arr.push("data2");
str_arr.push("data3");
puts(str_arr.join(","));
puts("----> 配列のサイズ")
puts(str_arr.length());
puts("----> 末尾を取り出す")
str_arr.pop();
puts(str_arr.join(","));
puts("----> 先頭に追加")
str_arr.unshift("data4");
puts(str_arr.join(","));
puts("----> 先頭を取り出す")
str_arr.shift();
puts(str_arr.join(","));
puts("----> ユニーク")
a = [1,2,3,4,5,1,2];
p a.uniq!()
puts("----> 一致する要素を削除")
a.delete(1)
p a
puts("----> map");
p array.map{|d| "DATA_" + d}
puts("----> select");
array2 = [1,2,3,4,5]
p array2.select{|d| d >=3}
puts("----> delete");
array2 = [1,2,3,4,5]
p array2.delete_if{|d| d >=3}
puts("----> 配列を探す");
array2 = [1,2,3,4,5]
p array2.index(3)
p array2.index(9)
puts("----> 配列の各要素にブロックを実行し配列を作成");
a = [1,2,3,4,5,6,7]
b = [1,2,3,4,5,6,7]
p a.collect!{|x| x*10}
p a
p b.collect{|x| x*10}
p b
puts("----> 分割")
p "1,2,3,4,5".split(",")
p "1\t2\t3\t4\t5".split("\t")
p "1+-+2+-+3+-+4+-+5".split("+-+")
puts("----> ハッシュ")
hash = Hash.new
hash["a"] = 1;
hash["b"] = 2;
hash["c"] = 3;
puts(hash["a"]);
puts (hash.has_key?("a"));
hash.delete("a");
puts("キーリスト")
puts(hash.keys());
puts("値リスト")
puts(hash.values());
puts("キーと値リスト")
hash.each{|key,value|
puts("#{key} - #{value}")
}
puts("メソッド")
def t_func1(x)
puts("<#{x}>")
end
p t_func1(2)
def t_func2(x, y)
return x+y
end
p t_func2(1,2).to_s
puts("--------------------> 集合")
require 'set'
fruits1 = Set["apple", "banana", "lemon"]
p fruits1
fruits1.add("melon")
p fruits1
fruits2 = Set["suica", "banana", "mikan"]
puts("和集合")
p fruits1 | fruits2
puts("積集合")
p fruits1 & fruits2
puts("差集合")
p fruits1 - fruits2
puts("--------------------> 出力")
puts("----> 出力フォーマット");
val1 = 1
str1= "baka"
puts "#{val1} - #{str1}"
printf("%8d\t%s",10,"aoi");
puts ("----> パーセント記法")
puts %q[あめんぼ["amenbo"]]
puts("--------------------> 正規表現")
str="rubytest";
r= Regexp.new("^ruby");
if r =~ str
puts("match");
elsif
puts("no match");
end
datas = "abc123abc456abc789".scan(/\d{3}/);
puts(datas);
puts("--------------------> 例外")
begin
dest = open("backup/passwd", "w")
rescue
puts("例外が発生したときの処理")
else
puts("例外が発生しなかったときに実行される処理")
ensure
puts("例外の発生有無に関わらず最後に必ず実行する処理")
end
puts("--------------------> sort")
puts("----> simple sort");
arr_datas = []
arr_datas.push("2")
arr_datas.push("1")
arr_datas.push("3")
p arr_datas.sort{|x,y| x.to_i <=> y.to_i}
puts("----> tab sort");
arr_datas=[]
arr_datas.push("a\t3");
arr_datas.push("c\t1");
arr_datas.push("d\t2");
arr_datas.push("b\t4");
puts(arr_datas.sort{|a,b| a.split("\t")[1] <=> b.split("\t")[1]});
puts("----> reverse");
puts(arr_datas.sort{|a,b| a.split("\t")[1] <=> b.split("\t")[1]}.reverse());
puts("----> hash sort");
hash = {};
hash["a"] = 2;
hash["c"] = 1;
hash["b"] = 3;
hash.sort{|a,b| a[0] <=>b [0]}.each{|a1,b1| puts("#{a1} - #{b1}")};
hash.sort{|a,b| a[1] <=> b[1]}.each{|name,value| puts("#{name} - #{value}")};
puts("--------------------> file")
require 'my_module'
include FileUtilty
puts("----> ファイルの読込書き込み")
data_lines = []
data_lines = read_file("test_utf8.txt","r:UTF-8")
write_file("save_test.txt","w:UTF-8",data_lines)
puts("----> ディレクトリ内の指定拡張子ファイルリストを取得")
files = get_filenames(".\\", '.txt')
files.each do |f|
puts(f)
end
puts("--------------------> class")
require '.\hello_count'
p HelloCount::VERSION
h = HelloCount.new("Hello sasaki")
h.Hello()
hk = HelloCountKeisho.new("Hello sasaki")
hk.Hello()
puts("--------------------> xml")
require 'rexml/document'
include REXML
doc = Document.new File.new("guitars.xml")
print(doc)
puts("")
puts "-------------------------------------------"
doc.elements.each("guitars/make/model/color"){|element| puts element.text}
puts "-------------------------------------------"
doc.elements.each("guitars/make") do |element|
puts element.name + ":"+ element.attributes["name"]
puts element.elements.size
element.elements.each("model") do |eleModel|
puts "\t" + eleModel.attributes["sn"]
puts "\t" + eleModel.attributes["year"]
puts "\t" + eleModel.attributes["country"]
puts "\t" + eleModel.elements["name"].text
puts "\t" + eleModel.elements["price"].text
puts "\t" + eleModel.elements["color"].text
puts ""
end
end
module FileUtilty
require 'find'
def read_file(file_path, mode)
data_lines=[]
File.open(file_path, mode) do |f|
while line = f.gets
data_lines.push(line.chomp())
end
end
return data_lines
end
def write_file(file_path, mode, lines)
File.open(file_path, mode) do |f|
lines.each do |line|
f.puts line;
end
end
end
def get_filenames(dir, extname)
files = []
Find.find(dir) do |f|
if File.file? f
if File.extname(f) == extname
files.push(f)
end
end
end
return files
end
end