| 情報学部 | 菅沼ホーム | 目次 | 索引 |
Float("1.0e-2") # => 0.01
Integer(123.5) # => 123
Integer("123.5") # => エラー
Integer(Float("123.5")) # => 123
String(123.5) # => "123.5" String(1.0e-2) # => "0.01"
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
例1 first test1 ret = 50 例2 second i = 1 i = 2 ret = 100
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
# 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()
p $_
p split("xx") "axxbxxc\n" ["a", "b", "c\n"]
x = 3.141592654; y = 123; z = "abc"
p sprintf("%6.4f%5d %s", x, y, z) "3.1416 123 abc"
system("ls-l")
system("ls", "-l")
p test(?s, "file1") # => 30 p test(?>, "file1", "file2") # => true
| 情報学部 | 菅沼ホーム | 目次 | 索引 |