ruby_learner 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/contents/questions/sequential_check/section_10/part_1/lib/answer.rb +5 -7
  4. data/contents/questions/sequential_check/section_10/part_1/lib/sentence.org +10 -8
  5. data/contents/questions/sequential_check/section_10/part_1/lib/workplace.rb +1 -1
  6. data/contents/questions/sequential_check/section_10/part_1/spec/workplace_spec.rb +4 -4
  7. data/contents/questions/sequential_check/section_10/part_2/lib/answer.rb +6 -7
  8. data/contents/questions/sequential_check/section_10/part_2/lib/sentence.org +9 -7
  9. data/contents/questions/sequential_check/section_10/part_2/lib/workplace.rb +1 -1
  10. data/contents/questions/sequential_check/section_10/part_2/spec/workplace_spec.rb +4 -9
  11. data/contents/questions/sequential_check/section_10/part_3/lib/answer.rb +12 -0
  12. data/contents/questions/sequential_check/section_10/part_3/lib/sentence.org +8 -0
  13. data/contents/questions/sequential_check/section_10/part_3/lib/workplace.rb +1 -0
  14. data/contents/questions/sequential_check/{section_6/part_4 → section_10/part_3}/spec/spec_helper.rb +0 -0
  15. data/contents/questions/sequential_check/section_10/part_3/spec/workplace_spec.rb +10 -0
  16. data/contents/questions/sequential_check/section_2/foo/nbar/nbaz/n +0 -0
  17. data/contents/questions/sequential_check/section_2/part_3/lib/answer.rb +3 -6
  18. data/contents/questions/sequential_check/section_2/part_3/lib/sentence.org +2 -0
  19. data/contents/questions/sequential_check/section_2/part_3/lib/workplace.rb +0 -4
  20. data/contents/questions/sequential_check/section_2/part_3/spec/workplace_spec.rb +8 -7
  21. data/contents/questions/sequential_check/section_3/part_2/lib/answer.rb +3 -5
  22. data/contents/questions/sequential_check/section_3/part_2/lib/workplace.rb +0 -4
  23. data/contents/questions/sequential_check/section_3/part_2/spec/workplace_spec.rb +8 -7
  24. data/contents/questions/sequential_check/section_4/part_2/lib/answer.rb +2 -2
  25. data/contents/questions/sequential_check/section_4/part_2/lib/workplace.rb +1 -1
  26. data/contents/questions/sequential_check/section_4/part_3/lib/answer.rb +8 -10
  27. data/contents/questions/sequential_check/section_4/part_3/lib/sentence.org +1 -0
  28. data/contents/questions/sequential_check/section_4/part_3/lib/workplace.rb +1 -3
  29. data/contents/questions/sequential_check/section_4/part_3/spec/workplace_spec.rb +6 -7
  30. data/contents/questions/sequential_check/section_6/part_2/lib/answer.rb +9 -4
  31. data/contents/questions/sequential_check/section_6/part_2/lib/sentence.org +35 -2
  32. data/contents/questions/sequential_check/section_6/part_2/lib/workplace.rb +2 -0
  33. data/contents/questions/sequential_check/section_6/part_2/spec/workplace_spec.rb +9 -10
  34. data/contents/questions/sequential_check/section_6/part_3/lib/answer.rb +5 -5
  35. data/contents/questions/sequential_check/section_6/part_3/lib/sentence.org +44 -5
  36. data/contents/questions/sequential_check/section_6/part_3/lib/workplace.rb +3 -0
  37. data/contents/questions/sequential_check/section_6/part_3/spec/workplace_spec.rb +2 -2
  38. data/contents/questions/sequential_check/section_7/part_1/lib/answer.rb +5 -3
  39. data/contents/questions/sequential_check/section_7/part_1/lib/sentence.org +22 -16
  40. data/contents/questions/sequential_check/section_7/part_1/spec/workplace_spec.rb +8 -8
  41. data/contents/questions/sequential_check/section_7/part_2/lib/answer.rb +6 -6
  42. data/contents/questions/sequential_check/section_7/part_2/lib/sentence.org +23 -22
  43. data/contents/questions/sequential_check/section_7/part_2/spec/workplace_spec.rb +11 -5
  44. data/contents/questions/sequential_check/section_7/part_3/lib/answer.rb +13 -0
  45. data/contents/questions/sequential_check/section_7/part_3/lib/sentence.org +24 -0
  46. data/contents/questions/sequential_check/section_7/part_3/lib/workplace.rb +4 -0
  47. data/contents/questions/sequential_check/section_7/part_3/spec/spec_helper.rb +100 -0
  48. data/contents/questions/sequential_check/section_7/part_3/spec/workplace_spec.rb +15 -0
  49. data/contents/questions/sequential_check/section_8/part_1/lib/answer.rb +11 -6
  50. data/contents/questions/sequential_check/section_8/part_1/lib/sentence.org +66 -10
  51. data/contents/questions/sequential_check/section_8/part_1/lib/workplace.rb +10 -0
  52. data/contents/questions/sequential_check/section_8/part_1/spec/workplace_spec.rb +28 -9
  53. data/contents/questions/sequential_check/section_8/part_2/lib/answer.rb +14 -0
  54. data/contents/questions/sequential_check/section_8/part_2/lib/sentence.org +80 -0
  55. data/contents/questions/sequential_check/section_8/part_2/lib/workplace.rb +1 -0
  56. data/contents/questions/sequential_check/section_8/part_2/spec/spec_helper.rb +100 -0
  57. data/contents/questions/sequential_check/section_8/part_2/spec/workplace_spec.rb +34 -0
  58. data/contents/questions/sequential_check/section_8/part_3/lib/answer.rb +20 -0
  59. data/contents/questions/sequential_check/section_8/part_3/lib/sentence.org +80 -0
  60. data/contents/questions/sequential_check/section_8/part_3/lib/workplace.rb +11 -0
  61. data/contents/questions/sequential_check/section_8/part_3/spec/spec_helper.rb +100 -0
  62. data/contents/questions/sequential_check/section_8/part_3/spec/workplace_spec.rb +34 -0
  63. data/contents/questions/sequential_check/section_9/part_1/lib/answer.rb +9 -4
  64. data/contents/questions/sequential_check/section_9/part_1/lib/sentence.org +93 -4
  65. data/contents/questions/sequential_check/section_9/part_1/spec/workplace_spec.rb +14 -5
  66. data/contents/questions/sequential_check/section_9/part_2/lib/answer.rb +8 -5
  67. data/contents/questions/sequential_check/section_9/part_2/lib/sentence.org +92 -10
  68. data/contents/questions/sequential_check/section_9/part_2/spec/workplace_spec.rb +14 -5
  69. data/contents/questions/sequential_check/section_9/part_3/lib/answer.rb +7 -9
  70. data/contents/questions/sequential_check/section_9/part_3/lib/sentence.org +93 -4
  71. data/contents/questions/sequential_check/section_9/part_3/spec/workplace_spec.rb +14 -5
  72. data/lib/ruby_learner/sequential_main.rb +25 -12
  73. data/lib/ruby_learner/version.rb +1 -1
  74. metadata +26 -7
  75. data/contents/questions/sequential_check/section_6/part_4/lib/answer.rb +0 -9
  76. data/contents/questions/sequential_check/section_6/part_4/lib/sentence.org +0 -12
  77. data/contents/questions/sequential_check/section_6/part_4/lib/workplace.rb +0 -1
  78. data/contents/questions/sequential_check/section_6/part_4/spec/workplace_spec.rb +0 -9
@@ -1,7 +1,7 @@
1
1
  # section_6/part_3/answer.rb
2
- # 6-3-array-size.rb
3
- nums = [3, 1, 4, 1, 5]
2
+ # 6-3-hash_mehods.rb
3
+ hash = { sasaki: 21, minami: 18, tanaka: 32 }
4
4
 
5
- print nums, "\n"
6
-
7
- print nums.size, "\n"
5
+ p hash.keys
6
+ p hash.values
7
+ p hash.to_a
@@ -1,9 +1,48 @@
1
- 配列を表示し、その配列の大きさも表示するプログラムを作成せよ。
2
- ただし、配列はプログラム内で定義すること。
1
+ -課題-
2
+ 与えられたハッシュ(hash)から,キーのみの配列,要素のみの配列,ハッシュを配列に変換したものを順にpを用いて出力せよ.
3
3
 
4
+ 今回の課題の解決策は以下のテキストに記載しているが,可能であるならば各自でネット検索を用いて解決してほしい.
4
5
 
5
- - sizeメソッド
6
+ 検索ワードヒント:ruby ハッシュ キー 要素 配列,ruby ハッシュ 配列 変換
6
7
 
7
- 配列名.size
8
+ * 配列
9
+ - 配列とは,いくつかのオブジェクトを順序つきで格納したオブジェクトです.格納されたオブジェクトは要素と呼びます.
10
+ ** 使用方法
11
+ - 配列作成...配列名 = [オブジェクト,オブジェクト,オブジェクト].オブジェクトは複数格納できるが何も格納しなくても良い.
12
+ - 要素呼び出し...配列名[index] (このindexとは配列の要素番号である.)
13
+ - 要素追加...配列 << 新しい要素
14
+ - 要素番号...配列の要素は0から数える.
15
+ - 要素変更...配列名[index] = オブジェクト
16
+ - 配列の大きさ(要素数)...配列名.size
8
17
 
9
- sizeメソッドを用いると、配列の大きさを得られる。
18
+ * ハッシュ
19
+ - ハッシュとは,いくつかのオブジェクトを文字列やシンボルをキーとして格納したオブジェクトです.格納されたオブジェクトは要素と呼びます.
20
+ ** 使用方法
21
+ - ハッシュ作成...ハッシュ名 = { キー => オブジェクト,キー => オブジェクト, ...}
22
+ ex) person = { name: "tanaka", age: 24}
23
+ (キーには数値や文字列やシンボル等が使用可能)
24
+ - 要素呼び出し...ハッシュ名[キー]
25
+ - 要素追加...ハッシュ名[新しいキー] = 新しいオブジェクト
26
+ - 要素変更...ハッシュ名[キー] = 変更後のオブジェクト
27
+ - ハッシュの大きさ(要素数)...ハッシュ名.size
28
+
29
+ ** ハッシュでのeach
30
+ - ハッシュ.each do |key, value|
31
+ ハッシュのキーと要素を取得可能
32
+ end
33
+
34
+ * シンボル
35
+ - 文字列に似たオブジェクトで,Rubyがメソッドなどの名前を識別するためのラベルをオブジェクトにしたものです.
36
+ - 使用方法...:文字列
37
+ ex) :hoge (シンボル「:hoge」)
38
+ :"hoge" (上記と同じ意味)
39
+ - シンボルと同様のことは文字列でもできます.
40
+ - シンボルは見た目は文字だが,実際は数字であるので,比較を行いやすい.
41
+
42
+ * 今回の課題の解決策
43
+ ** ハッシュからキーのみの配列を作る.
44
+ - ハッシュ.keys
45
+ ** ハッシュから要素のみの配列を作る.
46
+ - ハッシュ.values
47
+ ** ハッシュから配列への変換
48
+ - ハッシュ.to_a
@@ -1 +1,4 @@
1
1
  # section_6/part_3/workplace.rb
2
+ hash = { sasaki: 21, minami: 18, tanaka: 32 }
3
+
4
+ # add your code
@@ -1,9 +1,9 @@
1
1
  require "open3"
2
2
 
3
- RSpec.describe "array-size" do
3
+ RSpec.describe "hash_methods-size" do
4
4
  it 'put some datas' do
5
5
  workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
6
6
  stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb")
7
- expect { puts stdout }.to output("[3, 1, 4, 1, 5]\n5\n").to_stdout
7
+ expect { puts stdout }.to output("[:sasaki, :minami, :tanaka]\n[21, 18, 32]\n[[:sasaki, 21], [:minami, 18], [:tanaka, 32]]\n").to_stdout
8
8
  end
9
9
  end
@@ -1,5 +1,7 @@
1
1
  # section_7/part_1/answer.rb
2
- # 7-1-to_sym.rb
3
- n = ARGV[0]
2
+ # 7-1-def.rb
3
+ def hello
4
+ puts 'Hello, Ruby.'
5
+ end
4
6
 
5
- p n.to_sym
7
+ hello
@@ -1,16 +1,22 @@
1
- 1つの引数を受け取り、シンボルに変換して表示するプログラムを作成せよ。
2
-
3
-
4
- - シンボル
5
-
6
- 文字列に似たオブジェクトで、Rubyがメソッドなどの名前を識別するためのラベルをオブジェクトにしたもの。
7
- 先頭に「:」をつけて表現する。
8
-
9
-
10
- - to_symメソッド
11
-
12
- 文字列.to_sym
13
-
14
- 文字列をシンボルに変換するためのメソッド。
15
- 逆に、シンボルを文字列に変換するには、to_sメソッドを用いる。
16
-
1
+ -課題-
2
+ putsを用いて「Hello, Ruby.」を出力するhelloという名の関数を作成せよ.
3
+ また,その関数を実行せよ.
4
+
5
+ * オブジェクト指向
6
+ - オブジェクト指向とは問題の分析やシステムの設計あるいはプログラミングなど,システムやプログラム開発の場面で幅広く用いられています.
7
+ - ここでは,プログラミング言語Rubyにおけるオブジェクトとオブジェクト指向プログラミングの基礎について説明します.
8
+ 「プログラムの処理の対象(データ)を『オブジェクト』として考える」
9
+ この発想がオブジェクト指向には大切です.プログラムの処理の対象(データ)について,これまで取り上げてきた数値や文字列や配列といったものも簡単なデータと言えます.
10
+ オブジェクト指向言語における「オブジェクト」とは,こう言った何らかのデータあるいはデータの集合とそのデータを操作するための手続きをまとめたものです.
11
+ このように,データとそれを処理する手続きをオブジェクトとしてまとめることによって,全体の見通しが良くなります.
12
+ - 要するに,同じような処理を複数回行う場合に,何度も同じようなコードを1から作るのではなく,その処理ができる部品(オブジェクト)を用意してそれを何回も使いましょうという話です.
13
+
14
+ * 関数(メソッド)
15
+ - オブジェクト指向を実現するための部品作りを行える.
16
+ - メソッド内の処理を,メソッドを呼び出すごとに行える.
17
+ ** 使用方法
18
+ - メソッド作成...def メソッド名(引数)
19
+ メソッド内で実行したい処理
20
+ end
21
+ - メソッド呼び出し...メソッド名(引数)
22
+ - 引数はなくても良い.引数を設定すると,メソッド内でその引数を扱うことができる.
@@ -1,14 +1,14 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require "open3"
3
+ require "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
2
4
 
3
- RSpec.describe "symbol-check" do
4
- it 'given 2, return ":"2""' do
5
- workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
6
- stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb 2")
7
- expect { puts stdout }.to output(":\"2\"\n").to_stdout
5
+ RSpec.describe "def-check" do
6
+ it 'return "Hello, Ruby.\n"' do
7
+ expect { hello() }.to output("Hello, Ruby.\n").to_stdout
8
8
  end
9
- it 'given "hoge", return ":hoge"' do
9
+ it 'return "Hello, Ruby.\n"' do
10
10
  workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
11
- stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb hoge")
12
- expect { puts stdout }.to output(":hoge\n").to_stdout
11
+ stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb")
12
+ expect { puts stdout }.to output("Hello, Ruby.\n").to_stdout
13
13
  end
14
14
  end
@@ -1,9 +1,9 @@
1
1
  # section_7/part_2/answer.rb
2
- # 7-2-hash.rb
3
- address = { name: '高橋', furigana: 'タカハシ' }
2
+ # 7-2-def.rb
3
+ your_input = STDIN.gets.chomp
4
4
 
5
- puts address
5
+ def hello(name)
6
+ puts "Hello, #{name}."
7
+ end
6
8
 
7
- address[:tel] = '000-1234-5678'
8
-
9
- puts address
9
+ hello(your_input)
@@ -1,22 +1,23 @@
1
- :name, :furigana というそれぞれのキーに"高橋", "タカハシ"というオブジェクトが格納されているハッシュを作成、表示し、さらに:tel というキーに"000-1234-56778"を格納して再度表示するプログラムを作成せよ。
2
-
3
-
4
- - ハッシュ
5
-
6
- ハッシュ名 = {キー => オブジェクト, キー => オブジェクト, ...}
7
-
8
- 文字列やシンボルなどをキーにしてオブジェクトを格納するコンテナ。
9
- オブジェクトを取り出すためのキーと、それに対応させるオブジェクトの組からなる。
10
-
11
- また、キーがシンボルである場合、より短い書き方として、以下のような構文がある。
12
-
13
- ハッシュ名 = {キー: オブジェクト, キー: オブジェクト, ...}
14
-
15
-
16
- ハッシュに格納されたオブジェクトを取り出すには、次の構文を用いる。
17
-
18
- ハッシュ名[キー]
19
-
20
- また、オブジェクトを格納するには次の構文を用いる。
21
-
22
- ハッシュ名[キー] = 格納したいオブジェクト
1
+ -課題-
2
+ 文字列を引数にして「Hello, (引数).」をputsで出力する関数helloを作成せよ.
3
+ その後,キーボードから文字列を一つ受け取り,その値を引数にして関数helloを実行せよ.
4
+ キーボードから受け取る値には改行が含まれているので,その改行は削除すること.
5
+
6
+ * オブジェクト指向
7
+ - オブジェクト指向とは問題の分析やシステムの設計あるいはプログラミングなど,システムやプログラム開発の場面で幅広く用いられています.
8
+ - ここでは,プログラミング言語Rubyにおけるオブジェクトとオブジェクト指向プログラミングの基礎について説明します.
9
+ 「プログラムの処理の対象(データ)を『オブジェクト』として考える」
10
+ この発想がオブジェクト指向には大切です.プログラムの処理の対象(データ)について,これまで取り上げてきた数値や文字列や配列といったものも簡単なデータと言えます.
11
+ オブジェクト指向言語における「オブジェクト」とは,こう言った何らかのデータあるいはデータの集合とそのデータを操作するための手続きをまとめたものです.
12
+ このように,データとそれを処理する手続きをオブジェクトとしてまとめることによって,全体の見通しが良くなります.
13
+ - 要するに,同じような処理を複数回行う場合に,何度も同じようなコードを1から作るのではなく,その処理ができる部品(オブジェクト)を用意してそれを何回も使いましょうという話です.
14
+
15
+ * 関数(メソッド)
16
+ - オブジェクト指向を実現するための部品作りを行える.
17
+ - メソッド内の処理を,メソッドを呼び出すごとに行える.
18
+ ** 使用方法
19
+ - メソッド作成...def メソッド名(引数)
20
+ メソッド内で実行したい処理
21
+ end
22
+ - メソッド呼び出し...メソッド名(引数)
23
+ - 引数はなくても良い.引数を設定すると,メソッド内でその引数を扱うことができる.
@@ -1,10 +1,16 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require "open3"
3
3
 
4
- RSpec.describe "symbol-check" do
5
- it 'put some datas' do
6
- workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
7
- stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb")
8
- expect { puts stdout }.to output("{:name=>\"高橋\", :furigana=>\"タカハシ\"}\n{:name=>\"高橋\", :furigana=>\"タカハシ\", :tel=>\"000-1234-5678\"}\n").to_stdout
4
+ filename = "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
5
+
6
+ RSpec.describe "def-check" do
7
+ it 'given tanaka, return "Hello, tanaka.\n"' do
8
+ allow(STDIN).to receive(:gets) { "Ruby" }
9
+ require "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
10
+ expect { hello("tanaka") }.to output("Hello, tanaka.\n").to_stdout
11
+ end
12
+ it 'given kitaro\n, return "Hello, kitaro.\n"' do
13
+ stdout, stderr, status = Open3.capture3("ruby #{filename}",:stdin_data=>"kitaro\n")
14
+ expect { puts stdout }.to output("Hello, kitaro.\n").to_stdout
9
15
  end
10
16
  end
@@ -0,0 +1,13 @@
1
+ # section_7/part_3/answer.rb
2
+ # 7-3-def.rb
3
+ nums = [20, 40, 80, 100]
4
+
5
+ def average(array)
6
+ result = 0
7
+ array.each do |item|
8
+ result += item
9
+ end
10
+ result
11
+ end
12
+
13
+ puts average(nums)
@@ -0,0 +1,24 @@
1
+ -課題-
2
+ 配列を引数にして,その要素を全て足した値を戻り値にする関数averageを作成せよ.
3
+ 与えられた配列(nums)をその関数の引数に設定して,関数の結果をputsを用いて出力せよ.
4
+
5
+ * オブジェクト指向
6
+ - オブジェクト指向とは問題の分析やシステムの設計あるいはプログラミングなど,システムやプログラム開発の場面で幅広く用いられています.
7
+ - ここでは,プログラミング言語Rubyにおけるオブジェクトとオブジェクト指向プログラミングの基礎について説明します.
8
+ 「プログラムの処理の対象(データ)を『オブジェクト』として考える」
9
+ この発想がオブジェクト指向には大切です.プログラムの処理の対象(データ)について,これまで取り上げてきた数値や文字列や配列といったものも簡単なデータと言えます.
10
+ オブジェクト指向言語における「オブジェクト」とは,こう言った何らかのデータあるいはデータの集合とそのデータを操作するための手続きをまとめたものです.
11
+ このように,データとそれを処理する手続きをオブジェクトとしてまとめることによって,全体の見通しが良くなります.
12
+ - 要するに,同じような処理を複数回行う場合に,何度も同じようなコードを1から作るのではなく,その処理ができる部品(オブジェクト)を用意してそれを何回も使いましょうという話です.
13
+
14
+ * 関数(メソッド)
15
+ - オブジェクト指向を実現するための部品作りを行える.
16
+ - メソッド内の処理を,メソッドを呼び出すごとに行える.
17
+ ** 使用方法
18
+ - メソッド作成...def メソッド名(引数)
19
+ メソッド内で実行したい処理
20
+ end
21
+ - メソッド呼び出し...メソッド名(引数)
22
+ - メソッドの戻り値...メソッド内に「return 返したい値」で使用可能.
23
+ returnを省略した場合はメソッド内の最後の式を評価した値が戻り値となる.
24
+ - 引数はなくても良い.引数を設定すると,メソッド内でその引数を扱うことができる.
@@ -0,0 +1,4 @@
1
+ # section_7/part_3/workplace.rb
2
+ nums = [20, 40, 80, 100]
3
+
4
+ # add your code
@@ -0,0 +1,100 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
65
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
66
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
67
+ config.disable_monkey_patching!
68
+
69
+ # This setting enables warnings. It's recommended, but in some cases may
70
+ # be too noisy due to issues in dependencies.
71
+ config.warnings = true
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = "doc"
81
+ end
82
+
83
+ # Print the 10 slowest examples and example groups at the
84
+ # end of the spec run, to help surface which specs are running
85
+ # particularly slow.
86
+ config.profile_examples = 10
87
+
88
+ # Run specs in random order to surface order dependencies. If you find an
89
+ # order dependency and want to debug it, you can fix the order by providing
90
+ # the seed, which is printed after each run.
91
+ # --seed 1234
92
+ config.order = :random
93
+
94
+ # Seed global randomization in this process using the `--seed` CLI option.
95
+ # Setting this allows you to use `--seed` to deterministically reproduce
96
+ # test failures related to randomization by passing the same `--seed` value
97
+ # as the one that triggered the failure.
98
+ Kernel.srand config.seed
99
+ =end
100
+ end
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "open3"
3
+ require "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
4
+
5
+ filename = "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
6
+
7
+ RSpec.describe "def-check" do
8
+ it 'given [1, 2, 3], return 6' do
9
+ expect( average([1, 2, 3]) ).to eq(6)
10
+ end
11
+ it 'return "240\n"' do
12
+ stdout, stderr, status = Open3.capture3("ruby #{filename}")
13
+ expect { puts stdout }.to output("240\n").to_stdout
14
+ end
15
+ end
@@ -1,9 +1,14 @@
1
1
  # section_8/part_1/answer.rb
2
- # 8-1-match.rb
3
- n = /#{ARGV[0]}/
2
+ # 8-1-class.rb
3
+ class Hello
4
+ def initialize(name)
5
+ @name = name
6
+ end
4
7
 
5
- if n =~ 'Hello, Ruby.'
6
- print "match.\n"
7
- else
8
- print "not match.\n"
8
+ def stdout
9
+ puts "Hello, #{@name}."
10
+ end
9
11
  end
12
+
13
+ hello = Hello.new('tanaka')
14
+ hello.stdout
@@ -1,20 +1,76 @@
1
- 1つの文字列を受け取り、それが「Hello, Ruby」とマッチするなら「match.」マッチしないなら「not match.」と表示するプログラムを作成せよ。
1
+ -課題-
2
+ 与えられたクラス(Hello)を用いて,「Hello, tanaka.」と表示せよ.
3
+ インスタンスの作成にはnewメソッドを用いること.
4
+ * Class
5
+ - Classとはオブジェクトの種類を表すものです.
6
+ Rubyでは文字列や数値もStringクラスやIntクラスです.実際に皆さんが文字列を作成する場合は,Stringクラスのインスタンスを作成していることになります.
7
+ - Classと一言にいってもその内容は少し複雑なので,ここでは基礎的な内容を解説します.
2
8
 
9
+ ** インスタンス
10
+ - 作成したクラスを使用する場合に,作られるクラスの実体です.
11
+ その作られたクラスは一つのオブジェクトなので,そのオブジェクトをインスタンスと言います.
3
12
 
4
- - マッチング
13
+ ** 作成方法
14
+ - クラス名は必ず大文字で始めなければならない.
15
+ - 基本構造...class クラス名
16
+ クラスの定義(以下に説明する)
17
+ end
18
+ *** initializeメソッド...
19
+ - 機能...インスタンスが作成された時に最初に起動する処理を書く.引数はなくても良い.
20
+ - 使用方法...def initialize(引数)
21
+ 処理
22
+ end
5
23
 
6
- ある文字列が、特定の文字列のパターンに一致する部分を持っているか調べること。
7
- パターンに当てはまることを「マッチする」という。
24
+ *** インスタンスメソッド
25
+ - 機能...インスタンスから参照できる関数.
26
+ - 使用方法...通常の関数定義と同じ.
8
27
 
28
+ *** アクセスメソッド
29
+ - 機能...インスタンス外部からインスタンス変数に関する操作を行える関数.
30
+ - 以下の参照と更新を同時に行えるメソッド.呼び出し方法は以下の2種類とも使うことができる.
31
+ attr_accessor :変数名
32
+ **** インスタンス変数を参照
33
+ - 作成方法1...def 変数名
34
+ @変数名
35
+ end
36
+
37
+ - 作成方法2...(クラス内のメソッド外に以下を記述)
38
+ attr_reader :変数名
9
39
 
10
- - 正規表現
40
+ - 呼び出し方法...作成済みのインスタンス.変数名
11
41
 
12
- /パターン/
42
+ **** インスタンス変数の値を更新
43
+ - 作成方法1...def 変数名=(引数)
44
+ @変数名 = 引数
45
+ end
13
46
 
14
- マッチングを行う際に用いられる構文。
15
- 正規表現と文字列のマッチングを行うには、
47
+ - 作成方法2...(クラス内のメソッド外に以下を記述)
48
+ attr_writer :変数名
16
49
 
17
- /パターン/ =~ マッチングしたい文字列
50
+ - 呼び出し方法...作成済みのインスタンス.変数名 = 新しい値
18
51
 
19
- という構文を用いる。マッチングに成功すると、マッチした部分の位置を返す。
20
52
 
53
+ *** インスタンス変数
54
+ - 機能...作成されたインスタンス内で共有の変数.
55
+ - 使用方法...@変数名 (initializeメソッド内で定義されることが多い)
56
+
57
+ *** 特別な変数self
58
+ - インスタンスメソッド内で,メソッドのレシーバ自身(クラス自身)を参照する為に使われる変数.
59
+ - 使用方法...self.クラス内のメソッドや変数
60
+
61
+ *** クラス変数
62
+ - クラスからインスタンス化された全てのオブジェクト内で共有する変数.
63
+ - 使用方法...@@変数名
64
+
65
+ *** メソッドの呼び出しを制限する
66
+ - クラス内で,インスタンス化した場合に呼び出し可能にするかどうかを制限することができる.
67
+ 以下の3つの制御はインスタンスメソッドのdefの前にスペースを一つ開けて付け足すことで機能する.
68
+ - public...メソッドをインスタンスメソッドとして使えるように公開する.
69
+ - private...メソッドをレシーバを指定して呼び出せないようにする.
70
+ - protected...メソッドを同一のクラスであればインスタンスメソッドとして使えるようにする.
71
+
72
+ *** 継承
73
+ - 継承を行うことによって,既存のクラスには変更を加えずに,新しい機能を追加したり,部分的にカスタマイズしたりして新しいクラスを作ることができる.
74
+ - 使用方法...class 新しいクラス名 < 継承するクラス名
75
+ 追加するメソッド
76
+ end