-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-basic-print.rb
More file actions
133 lines (60 loc) · 1.95 KB
/
Copy path1-basic-print.rb
File metadata and controls
133 lines (60 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 出力系
print "Hello World", "world" #普通に表示
puts "Hello World", "again" #改行付きで表示
p "Hello World 3" #オブジェクト型が分かるように表示
# コメントは、頭に # を入れればOK。
=begin
コメント複数行
=の前に、tabや、スペースが入るとダメ。
Sublime Text なら、Command + / で
複数行 # のコメントアウトができるので、そちらの方が便利。
=end
# 変数
x = 3543;
p x;
p x = "hello".length
p "hello".length
x = Rational(3,4) # 分数 3/4という意味
x = 5 ** 20 # 5の20乗
x = Rational(2,3) + Rational(5,6) # 2/3 + 5/6 という意味(分数)
p 10.5.to_i # integer に型変換
p "10.5".to_i # integer に型変換
p 10.4.to_r #
p 10.5.round # ceil, floor
p rand(100) # 0-100 でランダム
p Math.sqrt(9) # Mathで、平方根
# 文字列関係
# 文字列オブジェクトに対する、操作
x = "hello" # 変数や特殊文字を展開
y = 'hello' # 展開しない
# 特殊文字 ¥n ¥t
puts "hello\nworld" # 改行が展開
puts 'hello\nworld' # 改行されない
name = "mio"
puts "my name is #{name}" # 名前が展開
# coding:utf-8
puts "こんにちわ" # 日本語出力がある場合は、# coding:utf-8 という行が必要
p "hello".upcase # 大文字変換
p "hello".reverse # 文字を逆順に
p "hello".index("o") # o が含まれる位置を返す
p "hello".include?"o" # o が含まれるかどうか、 true / false
s = "hello"
s1 = s.upcase! # 破壊的メソッド という。 s1 = s = s.upcase っていう意味。
p s
p s1
# 演算子について
a = [1,2,3,4]
b = [3,4,5,6]
# 演算子
p a & b # 共通部分だけを出す
p a | b # 重複しないように出す
p a - b
p a + b
# printf
x = "taguchi"
y = 25
printf("%s's score is %d \n", x, y)
printf("%s's score is %10d \n", x, y)
printf("%s's score is %-10d \n", x, y)
z = sprintf("%s's score is %010d \n", x, y)
p z