ruby_learner 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +58 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/new_terminal +25 -0
- data/bin/setup +8 -0
- data/docs/happy_ruby/RussOlsen_EloquentRuby_c1.pdf +0 -0
- data/docs/happy_ruby/RussOlsen_EloquentRuby_c5.pdf +0 -0
- data/docs/happy_ruby/TanoshiiRuby_v3_c23.pdf +0 -0
- data/docs/happy_ruby/TanoshiiRuby_v5_c1.pdf +0 -0
- data/docs/happy_ruby/TanoshiiRuby_v5_c2-3.pdf +0 -0
- data/docs/happy_ruby/c2.ipynb +479 -0
- data/docs/happy_ruby/c3_4.ipynb +237 -0
- data/docs/seminar/8-1.org +18 -0
- data/exe/ruby_learner +5 -0
- data/lib/ruby_learner/h.rb +14 -0
- data/lib/ruby_learner/methods.rb +131 -0
- data/lib/ruby_learner/random_h.rb +16 -0
- data/lib/ruby_learner/ruby_learner.rb +43 -0
- data/lib/ruby_learner/sequential_h.rb +15 -0
- data/lib/ruby_learner/typing_practice.rb +21 -0
- data/lib/ruby_learner/version.rb +3 -0
- data/questions/random_check/.rspec +1 -0
- data/questions/random_check/random_h.rb +16 -0
- data/questions/random_check/section_1/.rspec +1 -0
- data/questions/random_check/section_1/lib/answer.rb +15 -0
- data/questions/random_check/section_1/lib/sentence.org +9 -0
- data/questions/random_check/section_1/lib/workplace.rb +5 -0
- data/questions/random_check/section_1/spec/spec_helper.rb +100 -0
- data/questions/random_check/section_1/spec/workplace_spec.rb +10 -0
- data/questions/random_check/section_2/.rspec +1 -0
- data/questions/random_check/section_2/lib/answer.rb +17 -0
- data/questions/random_check/section_2/lib/sentence.org +12 -0
- data/questions/random_check/section_2/lib/workplace.rb +5 -0
- data/questions/random_check/section_2/spec/.rspec +1 -0
- data/questions/random_check/section_2/spec/spec_helper.rb +100 -0
- data/questions/random_check/section_2/spec/workplace_spec.rb +11 -0
- data/questions/sequential_check/section_1/part_1/lib/answer.rb +9 -0
- data/questions/sequential_check/section_1/part_1/lib/sentence.org +9 -0
- data/questions/sequential_check/section_1/part_1/lib/workplace.rb +5 -0
- data/questions/sequential_check/section_1/part_1/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_1/part_1/spec/workplace_spec.rb +10 -0
- data/questions/sequential_check/section_1/part_2/lib/answer.rb +16 -0
- data/questions/sequential_check/section_1/part_2/lib/sentence.org +12 -0
- data/questions/sequential_check/section_1/part_2/lib/workplace.rb +5 -0
- data/questions/sequential_check/section_1/part_2/spec/.rspec +1 -0
- data/questions/sequential_check/section_1/part_2/spec/spec_helper.rb +100 -0
- data/questions/sequential_check/section_1/part_2/spec/workplace_spec.rb +11 -0
- data/ruby_learner.gemspec +41 -0
- data/takahashi/docs/README.org +139 -0
- data/takahashi/docs/drill.html +875 -0
- data/takahashi/docs/drill.html~ +877 -0
- data/takahashi/docs/drill.org +446 -0
- data/takahashi/docs/ruby_for_beginner.html +2642 -0
- data/takahashi/docs/ruby_for_beginner.org +1430 -0
- data/takahashi/sample_prog/answer/10_1.rb +5 -0
- data/takahashi/sample_prog/answer/11_1.rb +5 -0
- data/takahashi/sample_prog/answer/11_2.rb +4 -0
- data/takahashi/sample_prog/answer/1_1.rb +1 -0
- data/takahashi/sample_prog/answer/1_2.rb +1 -0
- data/takahashi/sample_prog/answer/1_3.rb +1 -0
- data/takahashi/sample_prog/answer/2_1.rb +5 -0
- data/takahashi/sample_prog/answer/2_2.rb +12 -0
- data/takahashi/sample_prog/answer/3_1.rb +10 -0
- data/takahashi/sample_prog/answer/4_1.rb +7 -0
- data/takahashi/sample_prog/answer/5_1.rb +6 -0
- data/takahashi/sample_prog/answer/5_2.rb +3 -0
- data/takahashi/sample_prog/answer/6_1.rb +3 -0
- data/takahashi/sample_prog/answer/6_2.rb +5 -0
- data/takahashi/sample_prog/answer/6_3.rb +5 -0
- data/takahashi/sample_prog/answer/6_4.rb +7 -0
- data/takahashi/sample_prog/answer/7_1.rb +3 -0
- data/takahashi/sample_prog/answer/7_2.rb +8 -0
- data/takahashi/sample_prog/answer/9_1.rb +3 -0
- data/takahashi/sample_prog/answer/9_2.rb +5 -0
- data/takahashi/sample_prog/answer/9_3.rb +10 -0
- data/takahashi/sample_prog/answer/hello.rb +3 -0
- data/workshop/.rspec +1 -0
- data/workshop/emacs.d/ac-comphist.dat +50 -0
- data/workshop/emacs.d/cp5022x.el +156 -0
- data/workshop/emacs.d/elpa/archives/gnu/archive-contents +1240 -0
- data/workshop/emacs.d/elpa/archives/melpa/archive-contents +2 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/auto-complete-autoloads.el +65 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/auto-complete-config.el +551 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/auto-complete-config.elc +0 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/auto-complete-pkg.el +6 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/auto-complete.el +2164 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/auto-complete.elc +0 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/ada-mode +72 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/c++-mode +99 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/c-mode +55 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/caml-mode +231 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/clojure-mode +580 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/clojurescript-mode +475 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/coq-mode +278 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/css-mode +874 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/erlang-mode +216 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/ess-julia-mode +37 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/go-mode +25 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/haskell-mode +679 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/java-mode +53 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/js-mode +148 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/julia-mode +37 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/lua-mode +21 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/nim-mode +70 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/objc-mode +161 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/octave-mode +46 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/php-mode +6144 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/python-mode +379 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/qml-mode +183 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/ruby-mode +181 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/scala-mode +1347 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/scheme-mode +216 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/sclang-mode +1481 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/sh-mode +182 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/swift-mode +87 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/tcl-mode +172 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/ts-mode +797 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/tuareg-mode +231 -0
- data/workshop/emacs.d/elpa/auto-complete-20170124.1845/dict/verilog-mode +313 -0
- data/workshop/emacs.d/elpa/better-defaults-20170613.2104/better-defaults-autoloads.el +16 -0
- data/workshop/emacs.d/elpa/better-defaults-20170613.2104/better-defaults-pkg.el +2 -0
- data/workshop/emacs.d/elpa/better-defaults-20170613.2104/better-defaults.el +90 -0
- data/workshop/emacs.d/elpa/better-defaults-20170613.2104/better-defaults.elc +0 -0
- data/workshop/emacs.d/elpa/haml-mode-20170923.2153/haml-mode-autoloads.el +26 -0
- data/workshop/emacs.d/elpa/haml-mode-20170923.2153/haml-mode-pkg.el +2 -0
- data/workshop/emacs.d/elpa/haml-mode-20170923.2153/haml-mode.el +877 -0
- data/workshop/emacs.d/elpa/haml-mode-20170923.2153/haml-mode.elc +0 -0
- data/workshop/emacs.d/elpa/haml-mode-readme.txt +8 -0
- data/workshop/emacs.d/elpa/material-theme-20171123.1040/material-light-theme.el +918 -0
- data/workshop/emacs.d/elpa/material-theme-20171123.1040/material-light-theme.elc +0 -0
- data/workshop/emacs.d/elpa/material-theme-20171123.1040/material-theme-autoloads.el +32 -0
- data/workshop/emacs.d/elpa/material-theme-20171123.1040/material-theme-pkg.el +8 -0
- data/workshop/emacs.d/elpa/material-theme-20171123.1040/material-theme.el +912 -0
- data/workshop/emacs.d/elpa/material-theme-20171123.1040/material-theme.elc +0 -0
- data/workshop/emacs.d/elpa/ox-bibtex-chinese-readme.txt +21 -0
- data/workshop/emacs.d/elpa/popup-20160709.729/popup-autoloads.el +15 -0
- data/workshop/emacs.d/elpa/popup-20160709.729/popup-pkg.el +2 -0
- data/workshop/emacs.d/elpa/popup-20160709.729/popup.el +1432 -0
- data/workshop/emacs.d/elpa/popup-20160709.729/popup.elc +0 -0
- data/workshop/emacs.d/elpa/yaml-mode-20180212.1556/yaml-mode-autoloads.el +33 -0
- data/workshop/emacs.d/elpa/yaml-mode-20180212.1556/yaml-mode-pkg.el +2 -0
- data/workshop/emacs.d/elpa/yaml-mode-20180212.1556/yaml-mode.el +470 -0
- data/workshop/emacs.d/elpa/yaml-mode-20180212.1556/yaml-mode.elc +0 -0
- data/workshop/emacs.d/elpa/yaml-mode-readme.txt +25 -0
- data/workshop/emacs.d/haml-mode-master/.gitignore +1 -0
- data/workshop/emacs.d/haml-mode-master/.mailmap +2 -0
- data/workshop/emacs.d/haml-mode-master/MIT-LICENSE +20 -0
- data/workshop/emacs.d/haml-mode-master/README.md +47 -0
- data/workshop/emacs.d/haml-mode-master/haml-mode.el +887 -0
- data/workshop/emacs.d/iceberg_theme.el +202 -0
- data/workshop/emacs.d/init-open-recentf.el +133 -0
- data/workshop/emacs.d/init.el +229 -0
- data/workshop/emacs.d/inits/line-num.el +264 -0
- data/workshop/emacs.d/install-elisp.el +366 -0
- data/workshop/emacs.d/markdown-mode/markdown-mode.el +5978 -0
- data/workshop/emacs.d/notes +12 -0
- data/workshop/emacs.d/processing-mode/processing-mode.el +275 -0
- data/workshop/emacs.d/recentf +31 -0
- data/workshop/emacs.d/ruby-mode/inf-ruby.el +416 -0
- data/workshop/emacs.d/ruby-mode/rdoc-mode.el +130 -0
- data/workshop/emacs.d/ruby-mode/ruby-electric.el +205 -0
- data/workshop/emacs.d/ruby-mode/ruby-mode.el +1496 -0
- data/workshop/emacs.d/ruby-mode/ruby-style.el +78 -0
- data/workshop/emacs.d/ruby-mode/rubydb2x.el +104 -0
- data/workshop/emacs.d/ruby-mode/rubydb3x.el +115 -0
- data/workshop/emacs.d/ruby_learner_init.el +244 -0
- data/workshop/emacs.d/themes/dracula-theme.el +431 -0
- data/workshop/emacs.d/themes/iceberg-theme.el +205 -0
- data/workshop/emacs.d/themes/my-misterioso-theme.el +109 -0
- data/workshop/emacs.d/themes/my-wombat-theme.el +121 -0
- data/workshop/emacs.d/wiki-mode/wiki.el +976 -0
- data/workshop/emacs_help.org +34 -0
- data/workshop/lib/answer.rb +1 -0
- data/workshop/lib/sentence.org +1 -0
- data/workshop/lib/workplace.rb +1 -0
- data/workshop/restore/empty.rb +0 -0
- data/workshop/spec/spec_helper.rb +100 -0
- data/workshop/spec/workplace_spec.rb +1 -0
- data/workshop/training_data.txt +3 -0
- metadata +343 -0
@@ -0,0 +1,446 @@
|
|
1
|
+
#+OPTIONS: ^:{}
|
2
|
+
#+TITLE: はじめてのRuby
|
3
|
+
#+AUTHOR: Shun Takahashi
|
4
|
+
#+LANGUAGE: jp
|
5
|
+
#+OPTIONS: H:4 toc:t num:2
|
6
|
+
|
7
|
+
# -*- mode: org; -*-
|
8
|
+
|
9
|
+
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/htmlize.css"/>
|
10
|
+
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="http://www.pirilampo.org/styles/readtheorg/css/readtheorg.css"/>
|
11
|
+
|
12
|
+
#+HTML_HEAD: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
|
13
|
+
#+HTML_HEAD: <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
|
14
|
+
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/lib/js/jquery.stickytableheaders.js"></script>
|
15
|
+
#+HTML_HEAD: <script type="text/javascript" src="http://www.pirilampo.org/styles/readtheorg/js/readtheorg.js"></script>
|
16
|
+
# +SETUPFILE: /Users/bob/Github/org-html-themes/setup/theme-readtheorg-local-daddygongon.setup
|
17
|
+
|
18
|
+
|
19
|
+
# * drillを作ってみる
|
20
|
+
# 1. まず,Listを取り出す
|
21
|
+
# 1. 実行結果をつける
|
22
|
+
# 1. コードの振る舞いを記述するタイトルをつけていく.
|
23
|
+
|
24
|
+
|
25
|
+
* print,puts,pメソッド
|
26
|
+
|
27
|
+
** helloruby.rb
|
28
|
+
|
29
|
+
「Hello, Ruby.」という文字列を出力するプログラムを作成せよ
|
30
|
+
|
31
|
+
#+begin_src ruby
|
32
|
+
print('Hello, Ruby.\n')
|
33
|
+
#+end_src
|
34
|
+
|
35
|
+
#+begin_example
|
36
|
+
Hello, Ruby.
|
37
|
+
#+end_example
|
38
|
+
|
39
|
+
** helloruby_2.rb
|
40
|
+
|
41
|
+
「Hello, "Ruby".」という文字列を出力するプログラムを作成せよ
|
42
|
+
|
43
|
+
#+begin_src ruby
|
44
|
+
print("Hello, \"Ruby\".\n")
|
45
|
+
#+end_src
|
46
|
+
|
47
|
+
#+begin_example
|
48
|
+
> helloruby_2.rb
|
49
|
+
Hello,"Ruby".
|
50
|
+
#+end_example
|
51
|
+
|
52
|
+
** p_method.rb
|
53
|
+
|
54
|
+
「"Hello,\n\tRuby."」という文字列を出力するプログラムを作成せよ
|
55
|
+
|
56
|
+
#+begin_src ruby
|
57
|
+
p "Hello,\n\tRuby."
|
58
|
+
#+end_src
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
** kiritsubo.rb
|
63
|
+
|
64
|
+
# かな文字を出力で表示させるだけって必要?
|
65
|
+
|
66
|
+
* コマンドラインからのデータの入力
|
67
|
+
** print_argv.rb
|
68
|
+
|
69
|
+
コマンドラインから引数を5つ入力として受け取り、出力するプログラム
|
70
|
+
|
71
|
+
#+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]}"
|
78
|
+
#+end_src
|
79
|
+
|
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
|
88
|
+
#+end_example
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
** arg_arith.rb
|
93
|
+
|
94
|
+
コマンドラインから数値を2つ受け取り、和、差、積、商を表示するプログラム
|
95
|
+
|
96
|
+
#+begin_src ruby
|
97
|
+
num0 = ARGV[0].to_i
|
98
|
+
num1 = ARGV[1].to_i
|
99
|
+
|
100
|
+
puts "#{num0 + num1}"
|
101
|
+
puts "#{num0 - num1}"
|
102
|
+
puts "#{num0 * num1}"
|
103
|
+
puts "#{num0 / num1}"
|
104
|
+
#+end_src
|
105
|
+
|
106
|
+
#+begin_example
|
107
|
+
> ruby answer.rb 5 3
|
108
|
+
8
|
109
|
+
2
|
110
|
+
15
|
111
|
+
1
|
112
|
+
#+end_example
|
113
|
+
|
114
|
+
* 変数
|
115
|
+
|
116
|
+
** area_volume.rb
|
117
|
+
|
118
|
+
3つの数値x、y、zを受け取り、縦x、横y、高さzの直方体の表面積と体積を求めるプログラム
|
119
|
+
|
120
|
+
#+begin_src ruby
|
121
|
+
x = ARGV[0].to_i
|
122
|
+
y = ARGV[1].to_i
|
123
|
+
z = ARGV[2].to_i
|
124
|
+
|
125
|
+
area = (x*y + y*z + z*x) * 2
|
126
|
+
volume = x * y * z
|
127
|
+
|
128
|
+
print "表面積=", area, "\n"
|
129
|
+
print "体積=" volume, "\n"
|
130
|
+
#+end_src
|
131
|
+
|
132
|
+
#+begin_example
|
133
|
+
> ruby area_volume.rb 10 20 30
|
134
|
+
表面積=2200
|
135
|
+
体積=6000
|
136
|
+
#+end_example
|
137
|
+
|
138
|
+
* 条件判断
|
139
|
+
|
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
|
+
** greater_smaller_else.rb
|
166
|
+
|
167
|
+
List1.6のプログラムと同じ処理を、elseを用いて実行するプログラム
|
168
|
+
|
169
|
+
#+begin_src ruby
|
170
|
+
a = ARGV[0].to_i
|
171
|
+
|
172
|
+
if a >= 10
|
173
|
+
print "greater\n"
|
174
|
+
else
|
175
|
+
print "smaller\n"
|
176
|
+
#+end_src
|
177
|
+
|
178
|
+
#+begin_example ruby
|
179
|
+
>ruby greater_smaller_else.rb 20
|
180
|
+
greater
|
181
|
+
|
182
|
+
>ruby greater_smaller_else.rb 5
|
183
|
+
smaller
|
184
|
+
#+end_example
|
185
|
+
|
186
|
+
* 繰り返し
|
187
|
+
|
188
|
+
** 1_to_n.rb
|
189
|
+
|
190
|
+
入力された1つの数値nを引数として受け取り、1からnまでの数を順番に表示するプログラムを作成せよ
|
191
|
+
|
192
|
+
#+begin_src ruby
|
193
|
+
n = ARGV[0].to_i
|
194
|
+
i = 1
|
195
|
+
while i <= n
|
196
|
+
print i, "\n"
|
197
|
+
i = i + 1
|
198
|
+
end
|
199
|
+
#+end_src
|
200
|
+
|
201
|
+
#+begin_example ruby
|
202
|
+
> ruby 1_to_n.rb 5
|
203
|
+
1
|
204
|
+
2
|
205
|
+
3
|
206
|
+
4
|
207
|
+
5
|
208
|
+
#+end_example
|
209
|
+
|
210
|
+
** five_times.rb
|
211
|
+
|
212
|
+
# while文と動作での差別化難しい
|
213
|
+
|
214
|
+
「All work and no play makes Jack a dull boy.」と5行表示するプログラム
|
215
|
+
|
216
|
+
#+begin_src ruby
|
217
|
+
5.times do
|
218
|
+
print "All work and no play makes Jack a dull boy."
|
219
|
+
end
|
220
|
+
#+end_src
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
* 配列
|
225
|
+
|
226
|
+
** 配列からオブジェクトを取り出す
|
227
|
+
|
228
|
+
配列のn番目の要素を取り出し、表示するプログラム
|
229
|
+
|
230
|
+
#+begin_src ruby
|
231
|
+
names = ["小林", "林", "高野", "森岡"]
|
232
|
+
|
233
|
+
print names[ARGV[0]]
|
234
|
+
#+end_src
|
235
|
+
|
236
|
+
#+begin_src ruby
|
237
|
+
>ruby answer.rb 0
|
238
|
+
小林
|
239
|
+
#+end_src
|
240
|
+
|
241
|
+
** 配列にオブジェクトを格納する
|
242
|
+
|
243
|
+
配列の先頭の要素をコマンドラインに入力した要素に変更するプログラム
|
244
|
+
|
245
|
+
#+begin_src ruby
|
246
|
+
nums = [1, 2, 3]
|
247
|
+
|
248
|
+
nums[0] = ARGV[0]
|
249
|
+
|
250
|
+
print nums
|
251
|
+
#+end_src
|
252
|
+
|
253
|
+
|
254
|
+
#+begin_example
|
255
|
+
> ruby answer.rb 5
|
256
|
+
[5, 2, 3]
|
257
|
+
#+end_example
|
258
|
+
|
259
|
+
** 配列の大きさ
|
260
|
+
|
261
|
+
配列を表示し、その配列の大きさも表示するプログラムを作成せよ
|
262
|
+
|
263
|
+
#+begin_src ruby
|
264
|
+
names =
|
265
|
+
|
266
|
+
print names, "\n"
|
267
|
+
|
268
|
+
print names.size
|
269
|
+
#+end_src
|
270
|
+
|
271
|
+
** 配列と繰り返し
|
272
|
+
|
273
|
+
配列全体を表示してから、配列の要素を順に表示するプログラム
|
274
|
+
|
275
|
+
#+begin_src ruby
|
276
|
+
names = ["小林", "林", "高野", "森岡"]
|
277
|
+
|
278
|
+
print names, "\n"
|
279
|
+
|
280
|
+
names.each do |n|
|
281
|
+
puts n
|
282
|
+
end
|
283
|
+
#+end_src
|
284
|
+
|
285
|
+
#+begin_example
|
286
|
+
> ruby answer.rb
|
287
|
+
["小林", "林", "高野", "森岡"]
|
288
|
+
小林
|
289
|
+
林
|
290
|
+
高野
|
291
|
+
森岡
|
292
|
+
#+end_example
|
293
|
+
|
294
|
+
* ハッシュ
|
295
|
+
|
296
|
+
** シンボル
|
297
|
+
|
298
|
+
コマンドラインから文字列を受け取り、シンボルに変換して表示するプログラム
|
299
|
+
|
300
|
+
#+begin_src ruby
|
301
|
+
n = ARGV[0]
|
302
|
+
|
303
|
+
p n.to_sym
|
304
|
+
#+end_src
|
305
|
+
|
306
|
+
#+begin_example
|
307
|
+
> ruby answer.rb hash
|
308
|
+
:hash
|
309
|
+
#+end_example
|
310
|
+
|
311
|
+
** ハッシュの操作
|
312
|
+
|
313
|
+
:name, :furiganaというそれぞれのキーに"高橋", "タカハシ"というオブジェクトが格納されているハッシュを表示、さらに:telというキーに"000-1234-5678"を格納して再度表示するプログラム
|
314
|
+
|
315
|
+
#+begin_src ruby
|
316
|
+
address = {name: "高橋", furigana: "タカハシ"}
|
317
|
+
|
318
|
+
puts address
|
319
|
+
|
320
|
+
address[:tel] = "000-1234-5678"
|
321
|
+
|
322
|
+
puts address
|
323
|
+
#+end_src
|
324
|
+
|
325
|
+
#+begin_example
|
326
|
+
> ruby answer.rb
|
327
|
+
{:name=>"高橋", :furigana=>"タカハシ"}
|
328
|
+
{:name=>"高橋", :furigana=>"タカハシ", :tel=>"000-1234-5678"}
|
329
|
+
#+end_example
|
330
|
+
|
331
|
+
* 正規表現
|
332
|
+
|
333
|
+
** パターンとマッチング
|
334
|
+
|
335
|
+
#+begin_src ruby
|
336
|
+
|
337
|
+
|
338
|
+
#+end_src
|
339
|
+
|
340
|
+
* ファイルからの読み込み
|
341
|
+
|
342
|
+
** read_text.rb
|
343
|
+
|
344
|
+
コマンドラインからファイル名を受け取り、該当ファイルのテキストデータを表示するプログラム
|
345
|
+
|
346
|
+
#+begin_src ruby
|
347
|
+
filename = ARGV[0]
|
348
|
+
text = File.read(filename)
|
349
|
+
print text
|
350
|
+
#+end_src
|
351
|
+
|
352
|
+
#+begin_example
|
353
|
+
>ruby answer.rb [filename]
|
354
|
+
[text]
|
355
|
+
#+end_example
|
356
|
+
|
357
|
+
** read_line.rb
|
358
|
+
|
359
|
+
コマンドラインからファイル名を受け取り、該当ファイルのテキストデータを1行ずつ表示すつプログラム
|
360
|
+
|
361
|
+
#+begin_src ruby
|
362
|
+
filename = ARGV[0]
|
363
|
+
file = File.open(filename)
|
364
|
+
file.each_line do |line|
|
365
|
+
print line
|
366
|
+
end
|
367
|
+
#+end_src
|
368
|
+
|
369
|
+
#+begin_example
|
370
|
+
|
371
|
+
#+end_example
|
372
|
+
|
373
|
+
** simple_grep.rb
|
374
|
+
|
375
|
+
入力したテキストデータの中から、正規表現で指定した特定のパターンにマッチする行を出力するプログラム
|
376
|
+
|
377
|
+
#+begin_src ruby
|
378
|
+
pattern = Regexp.new(ARGV[0])
|
379
|
+
filename = ARGV[1]
|
380
|
+
|
381
|
+
file = File.open(filename)
|
382
|
+
file.each_line do |line|
|
383
|
+
if pattern =~ line
|
384
|
+
print line
|
385
|
+
end
|
386
|
+
end
|
387
|
+
file.close
|
388
|
+
#+end_src
|
389
|
+
|
390
|
+
#+begin_example
|
391
|
+
>ruby answer.rb [pattern] [filename]
|
392
|
+
#+end_example
|
393
|
+
|
394
|
+
* メソッドの作成
|
395
|
+
** hello_ruby2.rb
|
396
|
+
|
397
|
+
「Hello, Ruby.」と表示するメソッドhelloを用いて、
|
398
|
+
|
399
|
+
|
400
|
+
#+begin_src ruby
|
401
|
+
def hello
|
402
|
+
puts "Hello, Ruby."
|
403
|
+
end
|
404
|
+
|
405
|
+
hello()
|
406
|
+
#+end_src
|
407
|
+
|
408
|
+
#+begin_example
|
409
|
+
> ruby hello_ruby2.rb
|
410
|
+
Hello, Ruby.
|
411
|
+
#+end_example
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
* 別のファイルを取り込む
|
417
|
+
|
418
|
+
** ディレクトリ内のファイルの取り込み
|
419
|
+
#+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
|
428
|
+
end
|
429
|
+
#+end_src
|
430
|
+
|
431
|
+
#+begin_src ruby
|
432
|
+
require_relative "grep"
|
433
|
+
|
434
|
+
pattern = Regexp.new(ARGV[0])
|
435
|
+
filename = ARGV[1]
|
436
|
+
simple_grep(pattern, filename)
|
437
|
+
#+end_src
|
438
|
+
|
439
|
+
** Rubyの標準ライブラリ
|
440
|
+
|
441
|
+
#+begin_src ruby
|
442
|
+
require "date"
|
443
|
+
|
444
|
+
days = Date.today - Date.new(1993, 2, 24)
|
445
|
+
puts(days.to_i)
|
446
|
+
#+end_src
|