ruby_learner 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +17 -17
- data/{workshop → lib/datas}/emacs_help.org +0 -0
- data/lib/datas/final_history_sequential.txt +1 -0
- data/lib/ruby_learner/methods.rb +183 -24
- data/lib/ruby_learner/ruby_learner.rb +15 -7
- data/lib/ruby_learner/typing_practice.rb +6 -2
- data/lib/ruby_learner/version.rb +1 -1
- data/questions/sequential_check/section_1/part_1/lib/answer.rb +3 -9
- data/questions/sequential_check/section_1/part_1/lib/sentence.org +2 -9
- data/questions/sequential_check/section_1/part_1/lib/workplace.rb +1 -5
- data/questions/sequential_check/section_1/part_1/spec/workplace_spec.rb +6 -7
- data/questions/sequential_check/section_1/part_2/lib/answer.rb +4 -15
- data/questions/sequential_check/section_1/part_2/lib/sentence.org +2 -12
- data/questions/sequential_check/section_1/part_2/lib/workplace.rb +1 -5
- data/questions/sequential_check/section_1/part_2/spec/workplace_spec.rb +7 -9
- data/questions/sequential_check/section_1/part_3/lib/answer.rb +3 -0
- data/questions/sequential_check/section_1/part_3/lib/sentence.org +2 -0
- data/questions/sequential_check/section_1/part_3/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_1/part_3/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_1/part_3/spec/workplace_spec.rb +9 -0
- data/questions/sequential_check/section_10/part_1/lib/answer.rb +7 -0
- data/questions/sequential_check/section_10/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_10/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_10/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_10/part_1/spec/workplace_spec.rb +10 -0
- data/questions/sequential_check/section_10/part_2/lib/answer.rb +8 -0
- data/questions/sequential_check/section_10/part_2/lib/sentence.org +3 -0
- data/questions/sequential_check/section_10/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_10/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_10/part_2/spec/workplace_spec.rb +15 -0
- data/questions/sequential_check/section_11/part_1/lib/answer.rb +7 -0
- data/questions/sequential_check/section_11/part_1/lib/sentence.org +4 -0
- data/questions/sequential_check/section_11/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_11/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_11/part_1/spec/workplace_spec.rb +15 -0
- data/questions/sequential_check/section_11/part_2/lib/answer.rb +6 -0
- data/questions/sequential_check/section_11/part_2/lib/sentence.org +2 -0
- data/questions/sequential_check/section_11/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_11/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_11/part_2/spec/workplace_spec.rb +13 -0
- data/questions/sequential_check/section_2/part_1/lib/answer.rb +8 -0
- data/questions/sequential_check/section_2/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_2/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_2/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_2/part_1/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_2/part_2/lib/answer.rb +14 -0
- data/questions/sequential_check/section_2/part_2/lib/sentence.org +2 -0
- data/questions/sequential_check/section_2/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_2/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_2/part_2/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_3/part_1/lib/answer.rb +11 -0
- data/questions/sequential_check/section_3/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_3/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_3/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_3/part_1/spec/workplace_spec.rb +15 -0
- data/questions/sequential_check/section_4/part_1/lib/answer.rb +9 -0
- data/questions/sequential_check/section_4/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_4/part_1/lib/workplace.rb +2 -0
- data/questions/sequential_check/section_4/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_4/part_1/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_5/part_1/lib/answer.rb +8 -0
- data/questions/sequential_check/section_5/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_5/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_5/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_5/part_1/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_5/part_2/lib/answer.rb +5 -0
- data/questions/sequential_check/section_5/part_2/lib/sentence.org +2 -0
- data/questions/sequential_check/section_5/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_5/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_5/part_2/spec/workplace_spec.rb +9 -0
- data/questions/sequential_check/section_6/part_1/lib/answer.rb +5 -0
- data/questions/sequential_check/section_6/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_6/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_6/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_6/part_1/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_6/part_2/lib/answer.rb +7 -0
- data/questions/sequential_check/section_6/part_2/lib/sentence.org +3 -0
- data/questions/sequential_check/section_6/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_6/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_6/part_2/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_6/part_3/lib/answer.rb +7 -0
- data/questions/sequential_check/section_6/part_3/lib/sentence.org +3 -0
- data/questions/sequential_check/section_6/part_3/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_6/part_3/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_6/part_3/spec/workplace_spec.rb +9 -0
- data/questions/sequential_check/section_6/part_4/lib/answer.rb +9 -0
- data/questions/sequential_check/section_6/part_4/lib/sentence.org +3 -0
- data/questions/sequential_check/section_6/part_4/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_6/part_4/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_6/part_4/spec/workplace_spec.rb +9 -0
- data/questions/sequential_check/section_7/part_1/lib/answer.rb +5 -0
- data/questions/sequential_check/section_7/part_1/lib/sentence.org +3 -0
- data/questions/sequential_check/section_7/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_7/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_7/part_1/spec/workplace_spec.rb +14 -0
- data/questions/sequential_check/section_7/part_2/lib/answer.rb +9 -0
- data/questions/sequential_check/section_7/part_2/lib/sentence.org +2 -0
- data/questions/sequential_check/section_7/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_7/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_7/part_2/spec/workplace_spec.rb +10 -0
- data/questions/sequential_check/section_8/part_1/lib/answer.rb +9 -0
- data/questions/sequential_check/section_8/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_8/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_8/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_8/part_1/spec/workplace_spec.rb +15 -0
- data/questions/sequential_check/section_9/part_1/lib/answer.rb +5 -0
- data/questions/sequential_check/section_9/part_1/lib/sentence.org +2 -0
- data/questions/sequential_check/section_9/part_1/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_9/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_9/part_1/spec/workplace_spec.rb +10 -0
- data/questions/sequential_check/section_9/part_2/lib/answer.rb +7 -0
- data/questions/sequential_check/section_9/part_2/lib/sentence.org +2 -0
- data/questions/sequential_check/section_9/part_2/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_9/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_9/part_2/spec/workplace_spec.rb +10 -0
- data/questions/sequential_check/section_9/part_3/lib/answer.rb +12 -0
- data/questions/sequential_check/section_9/part_3/lib/sentence.org +2 -0
- data/questions/sequential_check/section_9/part_3/lib/workplace.rb +1 -0
- data/questions/sequential_check/section_9/part_3/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_9/part_3/spec/workplace_spec.rb +10 -0
- data/takahashi/docs/README.org +9 -0
- data/takahashi/docs/README.org~ +139 -0
- data/takahashi/docs/drill.html +189 -135
- data/takahashi/docs/drill.html~ +171 -187
- data/takahashi/docs/drill.org +163 -120
- data/takahashi/sample_prog/answer/10_2.rb +6 -0
- data/takahashi/sample_prog/answer/8_1.rb +9 -0
- data/takahashi/sample_prog/question/10_1.rb +2 -0
- data/takahashi/sample_prog/question/10_2.rb +3 -0
- data/takahashi/sample_prog/question/11_1.rb +4 -0
- data/takahashi/sample_prog/question/11_2.rb +2 -0
- data/takahashi/sample_prog/question/1_1.rb +2 -0
- data/takahashi/sample_prog/question/1_2.rb +2 -0
- data/takahashi/sample_prog/question/1_3.rb +2 -0
- data/takahashi/sample_prog/question/2_1.rb +2 -0
- data/takahashi/sample_prog/question/2_2.rb +2 -0
- data/takahashi/sample_prog/question/3_1.rb +2 -0
- data/takahashi/sample_prog/question/4_1.rb +2 -0
- data/takahashi/sample_prog/question/5_1.rb +2 -0
- data/takahashi/sample_prog/question/5_2.rb +2 -0
- data/takahashi/sample_prog/question/6_1.rb +2 -0
- data/takahashi/sample_prog/question/6_2.rb +3 -0
- data/takahashi/sample_prog/question/6_3.rb +3 -0
- data/takahashi/sample_prog/question/6_4.rb +3 -0
- data/takahashi/sample_prog/question/7_1.rb +3 -0
- data/takahashi/sample_prog/question/7_2.rb +2 -0
- data/takahashi/sample_prog/question/8_1.rb +2 -0
- data/takahashi/sample_prog/question/9_1.rb +2 -0
- data/takahashi/sample_prog/question/9_2.rb +2 -0
- data/takahashi/sample_prog/question/9_3.rb +2 -0
- data/workshop/emacs.d/#init# +1 -0
- data/workshop/emacs.d/init.el +8 -8
- data/workshop/emacs.d/ruby_learner_init.el +8 -8
- data/workshop/lib/for-def-read.rb +3 -0
- data/workshop/lib/for-read.txt +5 -0
- metadata +138 -7
- data/lib/ruby_learner/h.rb +0 -14
- data/lib/ruby_learner/random_h.rb +0 -16
- data/lib/ruby_learner/sequential_h.rb +0 -15
- data/workshop/training_data.txt +0 -3
data/takahashi/docs/drill.org
CHANGED
@@ -26,81 +26,83 @@
|
|
26
26
|
|
27
27
|
** helloruby.rb
|
28
28
|
|
29
|
-
「Hello, Ruby
|
29
|
+
「Hello, Ruby.」という文字列を表示するプログラムを作成せよ。
|
30
30
|
|
31
31
|
#+begin_src ruby
|
32
|
-
print
|
32
|
+
print 'Hello, Ruby.\n'
|
33
33
|
#+end_src
|
34
34
|
|
35
35
|
#+begin_example
|
36
|
+
> answer.rb
|
36
37
|
Hello, Ruby.
|
37
38
|
#+end_example
|
38
39
|
|
39
40
|
** helloruby_2.rb
|
40
41
|
|
41
|
-
「Hello, "Ruby"
|
42
|
+
「Hello, "Ruby".」という文字列を表示するプログラムを作成せよ。
|
42
43
|
|
43
44
|
#+begin_src ruby
|
44
|
-
print
|
45
|
+
print Hello, \"Ruby\".\n"
|
45
46
|
#+end_src
|
46
47
|
|
47
48
|
#+begin_example
|
48
|
-
>
|
49
|
-
Hello,"Ruby".
|
49
|
+
> answer.rb
|
50
|
+
Hello, "Ruby".
|
50
51
|
#+end_example
|
51
52
|
|
52
53
|
** p_method.rb
|
53
54
|
|
54
|
-
「"Hello,\n\tRuby."
|
55
|
+
「"Hello,\n\tRuby."」という文字列を表示するプログラムを作成せよ。
|
55
56
|
|
56
57
|
#+begin_src ruby
|
57
58
|
p "Hello,\n\tRuby."
|
58
59
|
#+end_src
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# かな文字を出力で表示させるだけって必要?
|
61
|
+
#+begin_example
|
62
|
+
> answer.rb
|
63
|
+
"Hello,\n\tRuby."
|
64
|
+
#+end_example
|
65
65
|
|
66
66
|
* コマンドラインからのデータの入力
|
67
|
+
|
67
68
|
** print_argv.rb
|
68
69
|
|
69
|
-
|
70
|
+
コマンドラインから5つの引数を受け取り、受け取った順に表示するプログラムを作成せよ。
|
70
71
|
|
71
72
|
#+begin_src ruby
|
72
|
-
|
73
|
-
puts
|
74
|
-
puts
|
75
|
-
puts
|
76
|
-
puts
|
77
|
-
puts "5番目の引数: #{ARGV[4]}"
|
73
|
+
puts ARGV[0]
|
74
|
+
puts ARGV[1]
|
75
|
+
puts ARGV[2]
|
76
|
+
puts ARGV[3]
|
77
|
+
puts ARGV[4]
|
78
78
|
#+end_src
|
79
79
|
|
80
80
|
#+begin_example
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
5番目の引数: 5th
|
81
|
+
> ruby answer.rb 1st 2nd 3rd 4th 5th
|
82
|
+
1st
|
83
|
+
2nd
|
84
|
+
3rd
|
85
|
+
4th
|
86
|
+
5th
|
88
87
|
#+end_example
|
89
88
|
|
90
|
-
|
91
|
-
|
92
89
|
** arg_arith.rb
|
93
90
|
|
94
|
-
コマンドラインから数値を2
|
91
|
+
コマンドラインから数値を2つ受け取り、その和、差、積、商を表示するプログラムを作成せよ。
|
95
92
|
|
96
93
|
#+begin_src ruby
|
97
94
|
num0 = ARGV[0].to_i
|
98
95
|
num1 = ARGV[1].to_i
|
99
96
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
97
|
+
sum = num0 + num1
|
98
|
+
dif = num0 - num1
|
99
|
+
pro = num0 * num1
|
100
|
+
quo = num0 / num1
|
101
|
+
|
102
|
+
puts sum
|
103
|
+
puts dif
|
104
|
+
puts pro
|
105
|
+
puts quo
|
104
106
|
#+end_src
|
105
107
|
|
106
108
|
#+begin_example
|
@@ -115,56 +117,31 @@ puts "#{num0 / num1}"
|
|
115
117
|
|
116
118
|
** area_volume.rb
|
117
119
|
|
118
|
-
3
|
120
|
+
3つの数値をx、y、zとして受け取り、縦x、横y、高さzの直方体の表面積と体積を求め、表示するプログラムを作成せよ。
|
119
121
|
|
120
122
|
#+begin_src ruby
|
121
123
|
x = ARGV[0].to_i
|
122
124
|
y = ARGV[1].to_i
|
123
125
|
z = ARGV[2].to_i
|
124
126
|
|
125
|
-
area = (x*y + y*z + z*x) * 2
|
127
|
+
area = (x * y + y * z + z * x) * 2
|
126
128
|
volume = x * y * z
|
127
129
|
|
128
|
-
print
|
129
|
-
print
|
130
|
+
print '表面積=', area, "\n"
|
131
|
+
print '体積=' volume, "\n"
|
130
132
|
#+end_src
|
131
133
|
|
132
134
|
#+begin_example
|
133
|
-
> ruby
|
135
|
+
> ruby answer.rb 10 20 30
|
134
136
|
表面積=2200
|
135
137
|
体積=6000
|
136
138
|
#+end_example
|
137
139
|
|
138
140
|
* 条件判断
|
139
141
|
|
140
|
-
** greater_smaller.rb
|
141
|
-
|
142
|
-
# List1.7だけで十分かも
|
143
|
-
|
144
|
-
数値を1つ引数として受け取り、それが10以上ならば「greater」、9以下ならば「smaller」と出力するプログラム
|
145
|
-
|
146
|
-
#+begin_src ruby
|
147
|
-
a = ARGV[0].to_i
|
148
|
-
|
149
|
-
if a >= 10
|
150
|
-
print "greater\n"
|
151
|
-
end
|
152
|
-
if a <= 9
|
153
|
-
print "smaller\n"
|
154
|
-
end
|
155
|
-
#+end_src
|
156
|
-
|
157
|
-
#+begin_example
|
158
|
-
>ruby greater_smaller_else.rb 20
|
159
|
-
greater
|
160
|
-
|
161
|
-
>ruby greater_smaller_else.rb 5
|
162
|
-
smaller
|
163
|
-
#+end_example
|
164
|
-
|
165
142
|
** greater_smaller_else.rb
|
166
143
|
|
167
|
-
|
144
|
+
1つの数値を受け取り、それが10より大きければ「greater」小さければ「smaller」と表示するプログラムを作成せよ。
|
168
145
|
|
169
146
|
#+begin_src ruby
|
170
147
|
a = ARGV[0].to_i
|
@@ -173,13 +150,14 @@ if a >= 10
|
|
173
150
|
print "greater\n"
|
174
151
|
else
|
175
152
|
print "smaller\n"
|
153
|
+
end
|
176
154
|
#+end_src
|
177
155
|
|
178
156
|
#+begin_example ruby
|
179
|
-
>ruby
|
157
|
+
>ruby answer.rb 20
|
180
158
|
greater
|
181
159
|
|
182
|
-
>ruby
|
160
|
+
>ruby answer.rb 5
|
183
161
|
smaller
|
184
162
|
#+end_example
|
185
163
|
|
@@ -187,19 +165,19 @@ smaller
|
|
187
165
|
|
188
166
|
** 1_to_n.rb
|
189
167
|
|
190
|
-
|
168
|
+
1つの数値をnとして受け取り、1からnまでの数を順番に表示するプログラムを作成せよ。
|
191
169
|
|
192
170
|
#+begin_src ruby
|
193
171
|
n = ARGV[0].to_i
|
194
172
|
i = 1
|
195
173
|
while i <= n
|
196
174
|
print i, "\n"
|
197
|
-
i
|
175
|
+
i += 1
|
198
176
|
end
|
199
177
|
#+end_src
|
200
178
|
|
201
179
|
#+begin_example ruby
|
202
|
-
> ruby
|
180
|
+
> ruby answer.rb 5
|
203
181
|
1
|
204
182
|
2
|
205
183
|
3
|
@@ -211,41 +189,53 @@ end
|
|
211
189
|
|
212
190
|
# while文と動作での差別化難しい
|
213
191
|
|
214
|
-
「All work and no play makes Jack a dull boy.」と5
|
192
|
+
「All work and no play makes Jack a dull boy.」と5行表示するプログラムを作成せよ。
|
215
193
|
|
216
194
|
#+begin_src ruby
|
217
195
|
5.times do
|
218
|
-
print "All work and no play makes Jack a dull boy
|
196
|
+
print "All work and no play makes Jack a dull boy.\n"
|
219
197
|
end
|
220
198
|
#+end_src
|
221
199
|
|
200
|
+
#+begin_example
|
201
|
+
> answer.rb
|
202
|
+
All work and no play makes Jack a dull boy.
|
203
|
+
All work and no play makes Jack a dull boy.
|
204
|
+
All work and no play makes Jack a dull boy.
|
205
|
+
All work and no play makes Jack a dull boy.
|
206
|
+
All work and no play makes Jack a dull boy.
|
207
|
+
#+end_example
|
222
208
|
|
223
209
|
|
224
210
|
* 配列
|
225
211
|
|
226
212
|
** 配列からオブジェクトを取り出す
|
227
213
|
|
228
|
-
|
214
|
+
1つの数値をnとして受け取り、配列のn番目の要素を取り出し、表示するプログラムを作成せよ。ただし、配列はプログラム内で定義し、配列には数値が入るものとする。
|
229
215
|
|
230
216
|
#+begin_src ruby
|
231
|
-
|
217
|
+
nums = [3, 1, 4, 1, 5]
|
232
218
|
|
233
|
-
print
|
219
|
+
print nums[ARGV[0].to_i]
|
234
220
|
#+end_src
|
235
221
|
|
236
|
-
#+
|
237
|
-
>ruby answer.rb 0
|
238
|
-
|
239
|
-
|
222
|
+
#+begin_example ruby
|
223
|
+
> ruby answer.rb 0
|
224
|
+
3
|
225
|
+
|
226
|
+
> ruby answer.rb 3
|
227
|
+
1
|
228
|
+
#+end_example
|
240
229
|
|
241
230
|
** 配列にオブジェクトを格納する
|
242
231
|
|
243
|
-
|
232
|
+
配列の先頭の要素をコマンドラインに入力した要素に変更するプログラムを作成せよ。
|
233
|
+
ただし、配列はプログラム内で定義し、配列には数値が入るものとする。
|
244
234
|
|
245
235
|
#+begin_src ruby
|
246
|
-
nums = [1,
|
236
|
+
nums = [3, 1, 4, 1, 5]
|
247
237
|
|
248
|
-
nums[0] = ARGV[0]
|
238
|
+
nums[0] = ARGV[0].to_i
|
249
239
|
|
250
240
|
print nums
|
251
241
|
#+end_src
|
@@ -253,49 +243,58 @@ print nums
|
|
253
243
|
|
254
244
|
#+begin_example
|
255
245
|
> ruby answer.rb 5
|
256
|
-
[5,
|
246
|
+
[5, 1, 4, 1, 5]
|
257
247
|
#+end_example
|
258
248
|
|
259
249
|
** 配列の大きさ
|
260
250
|
|
261
|
-
|
251
|
+
配列を表示し、その配列の大きさも表示するプログラムを作成せよ。
|
252
|
+
ただし、配列はプログラム内で定義すること。
|
262
253
|
|
263
254
|
#+begin_src ruby
|
264
|
-
|
255
|
+
nums = [3, 1, 4, 1, 5]
|
265
256
|
|
266
|
-
print
|
257
|
+
print nums, "\n"
|
267
258
|
|
268
|
-
print
|
259
|
+
print nums.size, "\n"
|
269
260
|
#+end_src
|
270
261
|
|
262
|
+
#+begin_example
|
263
|
+
> ruby answer.rb
|
264
|
+
[3, 1, 4, 1, 5]
|
265
|
+
5
|
266
|
+
#+end_example
|
267
|
+
|
271
268
|
** 配列と繰り返し
|
272
269
|
|
273
|
-
|
270
|
+
配列全体を表示してから、配列の要素を順に表示するプログラムを作成せよ。
|
271
|
+
ただし、配列はプログラム内で定義すること。
|
274
272
|
|
275
273
|
#+begin_src ruby
|
276
|
-
|
274
|
+
nums = [3, 1, 4, 1, 5]
|
277
275
|
|
278
|
-
print
|
276
|
+
print nums, "\n"
|
279
277
|
|
280
|
-
|
278
|
+
nums.each do |n|
|
281
279
|
puts n
|
282
280
|
end
|
283
281
|
#+end_src
|
284
282
|
|
285
283
|
#+begin_example
|
286
284
|
> ruby answer.rb
|
287
|
-
[
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
285
|
+
[3, 1, 4, 1, 5]
|
286
|
+
3
|
287
|
+
1
|
288
|
+
4
|
289
|
+
1
|
290
|
+
5
|
292
291
|
#+end_example
|
293
292
|
|
294
293
|
* ハッシュ
|
295
294
|
|
296
295
|
** シンボル
|
297
296
|
|
298
|
-
|
297
|
+
1つの文字列を受け取り、シンボルに変換して表示するプログラムを作成せよ。
|
299
298
|
|
300
299
|
#+begin_src ruby
|
301
300
|
n = ARGV[0]
|
@@ -310,10 +309,10 @@ p n.to_sym
|
|
310
309
|
|
311
310
|
** ハッシュの操作
|
312
311
|
|
313
|
-
:name, :furiganaというそれぞれのキーに"高橋", "タカハシ"
|
312
|
+
:name, :furigana というそれぞれのキーに"高橋", "タカハシ"というオブジェクトが格納されているハッシュを作成、表示し、さらに:tel というキーに"000-1234-5678"を格納して再度表示するプログラムを作成せよ。
|
314
313
|
|
315
314
|
#+begin_src ruby
|
316
|
-
address = {name: "高橋", furigana: "タカハシ"}
|
315
|
+
address = { name: "高橋", furigana: "タカハシ" }
|
317
316
|
|
318
317
|
puts address
|
319
318
|
|
@@ -332,16 +331,31 @@ puts address
|
|
332
331
|
|
333
332
|
** パターンとマッチング
|
334
333
|
|
335
|
-
|
334
|
+
1つの文字列を受け取り、それが「Hello, Ruby」とマッチするなら「match.」マッチしないなら「not match.」と表示するプログラムを作成せよ。
|
336
335
|
|
336
|
+
#+begin_src ruby
|
337
|
+
n = /#{ARGV[0]}/
|
337
338
|
|
339
|
+
if n =~ "Hello, Ruby."
|
340
|
+
print "match.\n"
|
341
|
+
else
|
342
|
+
print "not match\n"
|
343
|
+
end
|
338
344
|
#+end_src
|
339
345
|
|
346
|
+
#+begin_example
|
347
|
+
> ruby answer.rb Ruby
|
348
|
+
match.
|
349
|
+
|
350
|
+
> ruby answer.rb RUBY
|
351
|
+
not match.
|
352
|
+
#+end_example
|
353
|
+
|
340
354
|
* ファイルからの読み込み
|
341
355
|
|
342
356
|
** read_text.rb
|
343
357
|
|
344
|
-
|
358
|
+
1つの文字列を受け取り、該当するファイル名のテキストデータを表示するプログラムを作成せよ。
|
345
359
|
|
346
360
|
#+begin_src ruby
|
347
361
|
filename = ARGV[0]
|
@@ -356,7 +370,7 @@ print text
|
|
356
370
|
|
357
371
|
** read_line.rb
|
358
372
|
|
359
|
-
|
373
|
+
1つの文字列を受け取り、該当するファイル名のテキストデータを1行ずつ読み取り、表示するプログラムを作成せよ。
|
360
374
|
|
361
375
|
#+begin_src ruby
|
362
376
|
filename = ARGV[0]
|
@@ -372,7 +386,7 @@ end
|
|
372
386
|
|
373
387
|
** simple_grep.rb
|
374
388
|
|
375
|
-
|
389
|
+
2つの文字列を受け取り、2つ目の文字列に該当するファイル名のテキストデータの中から、1つ目の文字列にマッチする行を出力するプログラムを作成せよ。
|
376
390
|
|
377
391
|
#+begin_src ruby
|
378
392
|
pattern = Regexp.new(ARGV[0])
|
@@ -394,53 +408,82 @@ file.close
|
|
394
408
|
* メソッドの作成
|
395
409
|
** hello_ruby2.rb
|
396
410
|
|
397
|
-
「Hello, Ruby.」と表示するメソッドhello
|
398
|
-
|
411
|
+
「Hello, Ruby.」と表示するメソッドhelloを用いて、「Hello, Ruby.」と表示するプログラムを作成せよ。
|
399
412
|
|
400
413
|
#+begin_src ruby
|
401
414
|
def hello
|
402
|
-
puts
|
415
|
+
puts 'Hello, Ruby.'
|
403
416
|
end
|
404
417
|
|
405
|
-
hello
|
418
|
+
hello
|
406
419
|
#+end_src
|
407
420
|
|
408
421
|
#+begin_example
|
409
|
-
> ruby
|
422
|
+
> ruby answer.rb
|
410
423
|
Hello, Ruby.
|
411
424
|
#+end_example
|
412
425
|
|
426
|
+
** hello_ruby3.rb
|
413
427
|
|
428
|
+
1つの文字列を受け取り、「Hello, [受け取った文字列].」と表示するメソッドhelloを作成し、
|
429
|
+
2つの文字列を受け取り、「Hello, [受け取った文字列].」と2行表示するプログラムを作成せよ。
|
414
430
|
|
431
|
+
#+begin_src ruby
|
432
|
+
def hello(name)
|
433
|
+
print 'Hello, ', name, ".\n"
|
434
|
+
end
|
435
|
+
|
436
|
+
hello(ARGV[0])
|
437
|
+
hello(ARGV[1])
|
438
|
+
#+end_src
|
415
439
|
|
440
|
+
#+begin_example
|
441
|
+
> ruby answer.rb Ruby Takahashi
|
442
|
+
Hello, Ruby.
|
443
|
+
Hello, Takahashi.
|
444
|
+
#+end_example
|
416
445
|
* 別のファイルを取り込む
|
417
446
|
|
418
447
|
** ディレクトリ内のファイルの取り込み
|
448
|
+
|
449
|
+
1つの文字列を受け取り、「Hello, [受け取った文字列].」と表示するメソッドhelloが書かれたプログラムhello.rbがある。
|
450
|
+
|
451
|
+
このプログラムを取り込んで、1つの文字列を受け取り、「Hello, [受け取った文字列].」と表示するプログラムを作成せよ。
|
452
|
+
|
453
|
+
|
454
|
+
- hello.rb
|
419
455
|
#+begin_src ruby
|
420
|
-
def
|
421
|
-
|
422
|
-
file.each_line do |line|
|
423
|
-
if pattern =~ line
|
424
|
-
print line
|
425
|
-
end
|
426
|
-
end
|
427
|
-
file.close
|
456
|
+
def hello(name)
|
457
|
+
print 'Hello, ', name, "\n"
|
428
458
|
end
|
429
459
|
#+end_src
|
430
460
|
|
431
461
|
#+begin_src ruby
|
432
|
-
require_relative "
|
462
|
+
require_relative "hello"
|
433
463
|
|
434
|
-
|
435
|
-
|
436
|
-
|
464
|
+
name = ARGV[0]
|
465
|
+
|
466
|
+
hello(name)
|
437
467
|
#+end_src
|
438
468
|
|
469
|
+
#+begin_example
|
470
|
+
> answer.rb Ruby
|
471
|
+
Hello, Ruby.
|
472
|
+
#+end_example
|
473
|
+
|
439
474
|
** Rubyの標準ライブラリ
|
440
475
|
|
476
|
+
Rubyの標準ライブラリ「date」を用いて、Rubyが誕生した1993年2月24日から、今日までの日数を表示するプログラムを作成せよ。
|
477
|
+
|
441
478
|
#+begin_src ruby
|
442
479
|
require "date"
|
443
480
|
|
444
481
|
days = Date.today - Date.new(1993, 2, 24)
|
445
|
-
puts
|
482
|
+
puts days.to_i
|
446
483
|
#+end_src
|
484
|
+
|
485
|
+
#+begin_example
|
486
|
+
> ruby answer.rb
|
487
|
+
9302
|
488
|
+
#+end_example
|
489
|
+
- 2018/8/14時点
|