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.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +17 -17
  4. data/{workshop → lib/datas}/emacs_help.org +0 -0
  5. data/lib/datas/final_history_sequential.txt +1 -0
  6. data/lib/ruby_learner/methods.rb +183 -24
  7. data/lib/ruby_learner/ruby_learner.rb +15 -7
  8. data/lib/ruby_learner/typing_practice.rb +6 -2
  9. data/lib/ruby_learner/version.rb +1 -1
  10. data/questions/sequential_check/section_1/part_1/lib/answer.rb +3 -9
  11. data/questions/sequential_check/section_1/part_1/lib/sentence.org +2 -9
  12. data/questions/sequential_check/section_1/part_1/lib/workplace.rb +1 -5
  13. data/questions/sequential_check/section_1/part_1/spec/workplace_spec.rb +6 -7
  14. data/questions/sequential_check/section_1/part_2/lib/answer.rb +4 -15
  15. data/questions/sequential_check/section_1/part_2/lib/sentence.org +2 -12
  16. data/questions/sequential_check/section_1/part_2/lib/workplace.rb +1 -5
  17. data/questions/sequential_check/section_1/part_2/spec/workplace_spec.rb +7 -9
  18. data/questions/sequential_check/section_1/part_3/lib/answer.rb +3 -0
  19. data/questions/sequential_check/section_1/part_3/lib/sentence.org +2 -0
  20. data/questions/sequential_check/section_1/part_3/lib/workplace.rb +1 -0
  21. data/questions/sequential_check/section_1/part_3/spec/spec_helper.rb +100 -0
  22. data/questions/sequential_check/section_1/part_3/spec/workplace_spec.rb +9 -0
  23. data/questions/sequential_check/section_10/part_1/lib/answer.rb +7 -0
  24. data/questions/sequential_check/section_10/part_1/lib/sentence.org +2 -0
  25. data/questions/sequential_check/section_10/part_1/lib/workplace.rb +1 -0
  26. data/questions/sequential_check/section_10/part_1/spec/spec_helper.rb +100 -0
  27. data/questions/sequential_check/section_10/part_1/spec/workplace_spec.rb +10 -0
  28. data/questions/sequential_check/section_10/part_2/lib/answer.rb +8 -0
  29. data/questions/sequential_check/section_10/part_2/lib/sentence.org +3 -0
  30. data/questions/sequential_check/section_10/part_2/lib/workplace.rb +1 -0
  31. data/questions/sequential_check/section_10/part_2/spec/spec_helper.rb +100 -0
  32. data/questions/sequential_check/section_10/part_2/spec/workplace_spec.rb +15 -0
  33. data/questions/sequential_check/section_11/part_1/lib/answer.rb +7 -0
  34. data/questions/sequential_check/section_11/part_1/lib/sentence.org +4 -0
  35. data/questions/sequential_check/section_11/part_1/lib/workplace.rb +1 -0
  36. data/questions/sequential_check/section_11/part_1/spec/spec_helper.rb +100 -0
  37. data/questions/sequential_check/section_11/part_1/spec/workplace_spec.rb +15 -0
  38. data/questions/sequential_check/section_11/part_2/lib/answer.rb +6 -0
  39. data/questions/sequential_check/section_11/part_2/lib/sentence.org +2 -0
  40. data/questions/sequential_check/section_11/part_2/lib/workplace.rb +1 -0
  41. data/questions/sequential_check/section_11/part_2/spec/spec_helper.rb +100 -0
  42. data/questions/sequential_check/section_11/part_2/spec/workplace_spec.rb +13 -0
  43. data/questions/sequential_check/section_2/part_1/lib/answer.rb +8 -0
  44. data/questions/sequential_check/section_2/part_1/lib/sentence.org +2 -0
  45. data/questions/sequential_check/section_2/part_1/lib/workplace.rb +1 -0
  46. data/questions/sequential_check/section_2/part_1/spec/spec_helper.rb +100 -0
  47. data/questions/sequential_check/section_2/part_1/spec/workplace_spec.rb +14 -0
  48. data/questions/sequential_check/section_2/part_2/lib/answer.rb +14 -0
  49. data/questions/sequential_check/section_2/part_2/lib/sentence.org +2 -0
  50. data/questions/sequential_check/section_2/part_2/lib/workplace.rb +1 -0
  51. data/questions/sequential_check/section_2/part_2/spec/spec_helper.rb +100 -0
  52. data/questions/sequential_check/section_2/part_2/spec/workplace_spec.rb +14 -0
  53. data/questions/sequential_check/section_3/part_1/lib/answer.rb +11 -0
  54. data/questions/sequential_check/section_3/part_1/lib/sentence.org +2 -0
  55. data/questions/sequential_check/section_3/part_1/lib/workplace.rb +1 -0
  56. data/questions/sequential_check/section_3/part_1/spec/spec_helper.rb +100 -0
  57. data/questions/sequential_check/section_3/part_1/spec/workplace_spec.rb +15 -0
  58. data/questions/sequential_check/section_4/part_1/lib/answer.rb +9 -0
  59. data/questions/sequential_check/section_4/part_1/lib/sentence.org +2 -0
  60. data/questions/sequential_check/section_4/part_1/lib/workplace.rb +2 -0
  61. data/questions/sequential_check/section_4/part_1/spec/spec_helper.rb +100 -0
  62. data/questions/sequential_check/section_4/part_1/spec/workplace_spec.rb +14 -0
  63. data/questions/sequential_check/section_5/part_1/lib/answer.rb +8 -0
  64. data/questions/sequential_check/section_5/part_1/lib/sentence.org +2 -0
  65. data/questions/sequential_check/section_5/part_1/lib/workplace.rb +1 -0
  66. data/questions/sequential_check/section_5/part_1/spec/spec_helper.rb +100 -0
  67. data/questions/sequential_check/section_5/part_1/spec/workplace_spec.rb +14 -0
  68. data/questions/sequential_check/section_5/part_2/lib/answer.rb +5 -0
  69. data/questions/sequential_check/section_5/part_2/lib/sentence.org +2 -0
  70. data/questions/sequential_check/section_5/part_2/lib/workplace.rb +1 -0
  71. data/questions/sequential_check/section_5/part_2/spec/spec_helper.rb +100 -0
  72. data/questions/sequential_check/section_5/part_2/spec/workplace_spec.rb +9 -0
  73. data/questions/sequential_check/section_6/part_1/lib/answer.rb +5 -0
  74. data/questions/sequential_check/section_6/part_1/lib/sentence.org +2 -0
  75. data/questions/sequential_check/section_6/part_1/lib/workplace.rb +1 -0
  76. data/questions/sequential_check/section_6/part_1/spec/spec_helper.rb +100 -0
  77. data/questions/sequential_check/section_6/part_1/spec/workplace_spec.rb +14 -0
  78. data/questions/sequential_check/section_6/part_2/lib/answer.rb +7 -0
  79. data/questions/sequential_check/section_6/part_2/lib/sentence.org +3 -0
  80. data/questions/sequential_check/section_6/part_2/lib/workplace.rb +1 -0
  81. data/questions/sequential_check/section_6/part_2/spec/spec_helper.rb +100 -0
  82. data/questions/sequential_check/section_6/part_2/spec/workplace_spec.rb +14 -0
  83. data/questions/sequential_check/section_6/part_3/lib/answer.rb +7 -0
  84. data/questions/sequential_check/section_6/part_3/lib/sentence.org +3 -0
  85. data/questions/sequential_check/section_6/part_3/lib/workplace.rb +1 -0
  86. data/questions/sequential_check/section_6/part_3/spec/spec_helper.rb +100 -0
  87. data/questions/sequential_check/section_6/part_3/spec/workplace_spec.rb +9 -0
  88. data/questions/sequential_check/section_6/part_4/lib/answer.rb +9 -0
  89. data/questions/sequential_check/section_6/part_4/lib/sentence.org +3 -0
  90. data/questions/sequential_check/section_6/part_4/lib/workplace.rb +1 -0
  91. data/questions/sequential_check/section_6/part_4/spec/spec_helper.rb +100 -0
  92. data/questions/sequential_check/section_6/part_4/spec/workplace_spec.rb +9 -0
  93. data/questions/sequential_check/section_7/part_1/lib/answer.rb +5 -0
  94. data/questions/sequential_check/section_7/part_1/lib/sentence.org +3 -0
  95. data/questions/sequential_check/section_7/part_1/lib/workplace.rb +1 -0
  96. data/questions/sequential_check/section_7/part_1/spec/spec_helper.rb +100 -0
  97. data/questions/sequential_check/section_7/part_1/spec/workplace_spec.rb +14 -0
  98. data/questions/sequential_check/section_7/part_2/lib/answer.rb +9 -0
  99. data/questions/sequential_check/section_7/part_2/lib/sentence.org +2 -0
  100. data/questions/sequential_check/section_7/part_2/lib/workplace.rb +1 -0
  101. data/questions/sequential_check/section_7/part_2/spec/spec_helper.rb +100 -0
  102. data/questions/sequential_check/section_7/part_2/spec/workplace_spec.rb +10 -0
  103. data/questions/sequential_check/section_8/part_1/lib/answer.rb +9 -0
  104. data/questions/sequential_check/section_8/part_1/lib/sentence.org +2 -0
  105. data/questions/sequential_check/section_8/part_1/lib/workplace.rb +1 -0
  106. data/questions/sequential_check/section_8/part_1/spec/spec_helper.rb +100 -0
  107. data/questions/sequential_check/section_8/part_1/spec/workplace_spec.rb +15 -0
  108. data/questions/sequential_check/section_9/part_1/lib/answer.rb +5 -0
  109. data/questions/sequential_check/section_9/part_1/lib/sentence.org +2 -0
  110. data/questions/sequential_check/section_9/part_1/lib/workplace.rb +1 -0
  111. data/questions/sequential_check/section_9/part_1/spec/spec_helper.rb +100 -0
  112. data/questions/sequential_check/section_9/part_1/spec/workplace_spec.rb +10 -0
  113. data/questions/sequential_check/section_9/part_2/lib/answer.rb +7 -0
  114. data/questions/sequential_check/section_9/part_2/lib/sentence.org +2 -0
  115. data/questions/sequential_check/section_9/part_2/lib/workplace.rb +1 -0
  116. data/questions/sequential_check/section_9/part_2/spec/spec_helper.rb +100 -0
  117. data/questions/sequential_check/section_9/part_2/spec/workplace_spec.rb +10 -0
  118. data/questions/sequential_check/section_9/part_3/lib/answer.rb +12 -0
  119. data/questions/sequential_check/section_9/part_3/lib/sentence.org +2 -0
  120. data/questions/sequential_check/section_9/part_3/lib/workplace.rb +1 -0
  121. data/questions/sequential_check/section_9/part_3/spec/spec_helper.rb +100 -0
  122. data/questions/sequential_check/section_9/part_3/spec/workplace_spec.rb +10 -0
  123. data/takahashi/docs/README.org +9 -0
  124. data/takahashi/docs/README.org~ +139 -0
  125. data/takahashi/docs/drill.html +189 -135
  126. data/takahashi/docs/drill.html~ +171 -187
  127. data/takahashi/docs/drill.org +163 -120
  128. data/takahashi/sample_prog/answer/10_2.rb +6 -0
  129. data/takahashi/sample_prog/answer/8_1.rb +9 -0
  130. data/takahashi/sample_prog/question/10_1.rb +2 -0
  131. data/takahashi/sample_prog/question/10_2.rb +3 -0
  132. data/takahashi/sample_prog/question/11_1.rb +4 -0
  133. data/takahashi/sample_prog/question/11_2.rb +2 -0
  134. data/takahashi/sample_prog/question/1_1.rb +2 -0
  135. data/takahashi/sample_prog/question/1_2.rb +2 -0
  136. data/takahashi/sample_prog/question/1_3.rb +2 -0
  137. data/takahashi/sample_prog/question/2_1.rb +2 -0
  138. data/takahashi/sample_prog/question/2_2.rb +2 -0
  139. data/takahashi/sample_prog/question/3_1.rb +2 -0
  140. data/takahashi/sample_prog/question/4_1.rb +2 -0
  141. data/takahashi/sample_prog/question/5_1.rb +2 -0
  142. data/takahashi/sample_prog/question/5_2.rb +2 -0
  143. data/takahashi/sample_prog/question/6_1.rb +2 -0
  144. data/takahashi/sample_prog/question/6_2.rb +3 -0
  145. data/takahashi/sample_prog/question/6_3.rb +3 -0
  146. data/takahashi/sample_prog/question/6_4.rb +3 -0
  147. data/takahashi/sample_prog/question/7_1.rb +3 -0
  148. data/takahashi/sample_prog/question/7_2.rb +2 -0
  149. data/takahashi/sample_prog/question/8_1.rb +2 -0
  150. data/takahashi/sample_prog/question/9_1.rb +2 -0
  151. data/takahashi/sample_prog/question/9_2.rb +2 -0
  152. data/takahashi/sample_prog/question/9_3.rb +2 -0
  153. data/workshop/emacs.d/#init# +1 -0
  154. data/workshop/emacs.d/init.el +8 -8
  155. data/workshop/emacs.d/ruby_learner_init.el +8 -8
  156. data/workshop/lib/for-def-read.rb +3 -0
  157. data/workshop/lib/for-read.txt +5 -0
  158. metadata +138 -7
  159. data/lib/ruby_learner/h.rb +0 -14
  160. data/lib/ruby_learner/random_h.rb +0 -16
  161. data/lib/ruby_learner/sequential_h.rb +0 -15
  162. data/workshop/training_data.txt +0 -3
@@ -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('Hello, Ruby.\n')
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("Hello, \"Ruby\".\n")
45
+ print Hello, \"Ruby\".\n"
45
46
  #+end_src
46
47
 
47
48
  #+begin_example
48
- > helloruby_2.rb
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
- ** kiritsubo.rb
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
- コマンドラインから引数を5つ入力として受け取り、出力するプログラム
70
+ コマンドラインから5つの引数を受け取り、受け取った順に表示するプログラムを作成せよ。
70
71
 
71
72
  #+begin_src ruby
72
- # List3.1 print_argv.rb
73
- puts "最初の引数: #{ARGV[0]}"
74
- puts "2番目の引数: #{ARGV[1]}"
75
- puts "3番目の引数: #{ARGV[2]}"
76
- puts "4番目の引数: #{ARGV[3]}"
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
- > ruby print_argv.rb 1st 2nd 3rd 4th 5th
83
- 最初の引数: 1st
84
- 2番目の引数: 2nd
85
- 3番目の引数: 3rd
86
- 4番目の引数: 4th
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
- puts "#{num0 + num1}"
101
- puts "#{num0 - num1}"
102
- puts "#{num0 * num1}"
103
- puts "#{num0 / num1}"
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つの数値x、y、zを受け取り、縦x、横y、高さzの直方体の表面積と体積を求めるプログラム
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 "表面積=", area, "\n"
129
- print "体積=" volume, "\n"
130
+ print '表面積=', area, "\n"
131
+ print '体積=' volume, "\n"
130
132
  #+end_src
131
133
 
132
134
  #+begin_example
133
- > ruby area_volume.rb 10 20 30
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
- List1.6のプログラムと同じ処理を、elseを用いて実行するプログラム
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 greater_smaller_else.rb 20
157
+ >ruby answer.rb 20
180
158
  greater
181
159
 
182
- >ruby greater_smaller_else.rb 5
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
- 入力された1つの数値nを引数として受け取り、1からnまでの数を順番に表示するプログラムを作成せよ
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 = i + 1
175
+ i += 1
198
176
  end
199
177
  #+end_src
200
178
 
201
179
  #+begin_example ruby
202
- > ruby 1_to_n.rb 5
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
- 配列のn番目の要素を取り出し、表示するプログラム
214
+ 1つの数値をnとして受け取り、配列のn番目の要素を取り出し、表示するプログラムを作成せよ。ただし、配列はプログラム内で定義し、配列には数値が入るものとする。
229
215
 
230
216
  #+begin_src ruby
231
- names = ["小林", "林", "高野", "森岡"]
217
+ nums = [3, 1, 4, 1, 5]
232
218
 
233
- print names[ARGV[0]]
219
+ print nums[ARGV[0].to_i]
234
220
  #+end_src
235
221
 
236
- #+begin_src ruby
237
- >ruby answer.rb 0
238
- 小林
239
- #+end_src
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, 2, 3]
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, 2, 3]
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
- names =
255
+ nums = [3, 1, 4, 1, 5]
265
256
 
266
- print names, "\n"
257
+ print nums, "\n"
267
258
 
268
- print names.size
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
- names = ["小林", "林", "高野", "森岡"]
274
+ nums = [3, 1, 4, 1, 5]
277
275
 
278
- print names, "\n"
276
+ print nums, "\n"
279
277
 
280
- names.each do |n|
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というそれぞれのキーに"高橋", "タカハシ"というオブジェクトが格納されているハッシュを表示、さらに:telというキーに"000-1234-5678"を格納して再度表示するプログラム
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
- #+begin_src ruby
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
- コマンドラインからファイル名を受け取り、該当ファイルのテキストデータを1行ずつ表示すつプログラム
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 "Hello, Ruby."
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 hello_ruby2.rb
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 simple_grep(pattern, filename)
421
- file = File.open(filename)
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 "grep"
462
+ require_relative "hello"
433
463
 
434
- pattern = Regexp.new(ARGV[0])
435
- filename = ARGV[1]
436
- simple_grep(pattern, filename)
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(days.to_i)
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時点