静岡理工科大学 | 菅沼ホーム | Ruby 目次 | 索引 |
例1 first test1 ret = 50 例2 second i = 1 i = 2 ret = 100
printf " 例1\n" ret = catch(:test1) { printf "first %s\n", :test1 50 } printf "ret = %d\n", ret printf " 例2\n" ret = catch(:test2) { printf "second\n" for i in [1, 2, 3] printf "i = %d\n", i if i == 2 throw :test2, 100 end end printf "third\n" } printf "ret = %d\n", ret
# chop gets # a を入力 p $_ # 出力は "a\n" p chop # "\n" を取り除き,出力は "a" p chop # "a" を取り除き,出力は "" p chop # 出力は "" # chop! gets # a を入力 p $_ # 出力は "a\n" p chop! # "\n" を取り除き,出力は "a" p chop! # "a" を取り除き,出力は "" p chop! # 出力は nil
# chomp gets # abc を入力 p $_ # 出力は "abc\n" p chomp("\n") # "\n" を取り除き,出力は "abc" p chomp("bc") # "bc" を取り除き,出力は "a" p chomp("bc") # 出力は "a" # chomp! gets # abc を入力 p $_ # 出力は "abc\n" p chomp!("\n") # "\n" を取り除き,出力は "abc" p chomp!("bc") # "bc" を取り除き,出力は "a" p chomp!("bc") # 出力は nil
a = 1 b = 2 def func a = 10 b = 20 binding end eval("p a+b") # 3 を出力 eval("p a+b", func) # 30 を出力
exec("ls -l") exec("ls", "-l")
printf "parent process\n" fork {printf "child process\n"}
data1 in file1 data2 in file1
data1 in file2
ruby test.rb file1 file2
printf "%s %s\n", ARGV[0], ARGV[1] while line = gets printf "%s %s\n", ARGV[0], ARGV[1] printf " %s", line end p gets p ARGF.filename
c:\temp>ruby test.rb file1 file2 file1 file2 file2 data1 in file1 file2 data2 in file1 data1 in file2 nil "file2"
ruby test.rb file1
["data1 in file1\n", "data2 in file1\n"]
a = readlines p a
$_ = "agbcdefdg" p gsub(/d/, "xxx") p gsub(/h/, "yyy") p gsub!(/h/, "yyy") p gsub(/g/) { |m| m.replace("yyy") }
"agbcxxxefxxxg" "agbcxxxefxxxg" nil "ayyybcxxxefxxxyyy"
# main (test.rb) printf "main file\n" load("test1.rb") #loaded file (test1.rb) printf "loaded file\n"
main file loaded file
x = 0 loop { x += 1 printf "%d\n", x if x == 3 break end }
# ブロックを使用しない obj = open("file1", "r") while line = obj.gets printf "%s", line end obj.close() # ブロックを使用する open("file1", "r") { |obj| while line = obj.gets printf "%s", line end }
obj = open("|ls -l") while line = obj.gets printf "%s", line # 「ls -l」を実行した結果を出力 end
a = ["abc", "123", "xyz"] b = [10, 20, 30] $> = open("file3", "w") # 出力先の変更 printf " p による出力(ファイル file3)\n" p a, b printf " print による出力(ファイル file3)\n" print a, "\n", b, "\n" print a.inspect, "\n", b.inspect, "\n" out = open("file4", "w") # 出力先の変更 printf out, " printf による出力(ファイル file4)\n" printf out, "%s\n%d%d%d\n", a, b[0], b[1], b[2] printf out, "[\"%s\", \"%s\", \"%s\"]\n", a[0], a[1], a[2] printf out, "[\"%d\", \"%d\", \"%d\"]\n", b[0], b[1], b[2]
p による出力(ファイル file3) ["abc", "123", "xyz"] [10, 20, 30] print による出力(ファイル file3) abc123xyz 102030 ["abc", "123", "xyz"] [10, 20, 30] printf による出力(ファイル file4) abc123xyz 102030 ["abc", "123", "xyz"] ["10", "20", "30"]
a = ["abc", "123", "xyz"] b = [10, 20, 30] printf " print による出力\n" print a, "\n", b, "\n" printf " puts による出力\n" puts a, b
print による出力 abc123xyz 102030 puts による出力 abc 123 xyz 10 20 30
gets() print "$_ の内容 ", $_ p scan(/a./) p scan(/(a.)/) scan(/a./) { |x| p x } scan(/(a.)/) { |x| p x }
$_ の内容 abcaxy ["ab", "ax"] [["ab"], ["ax"]] "ab" "ax" ["ab"] ["ax"]
"axxbxxc\n" ["a", "b", "c\n"]
gets() p $_ p split("xx")
"3.1416 123 abc"
x = 3.141592654; y = 123; z = "abc" p sprintf("%6.4f%5d %s", x, y, z)
$_ = "agbcdefdg" p sub(/d/, "xxx") p sub(/h/, "yyy") p sub!(/h/, "yyy") p sub(/g/) { |m| m.replace("yyy") }
"agbcxxxefdg" "agbcxxxefdg" nil "ayyybcxxxefdg"
system("ls-l") system("ls", "-l")
p test(?s, "file1") # => 30 p test(?>, "file1", "file2") # => true