ruby_learner 1.1.14 → 1.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/ruby_learner/sequential_main.rb +19 -5
  4. data/lib/ruby_learner/version.rb +1 -1
  5. data/questions/sequential_check/section_1/part_1/lib/sentence.org +17 -6
  6. data/questions/sequential_check/section_1/part_1/spec/workplace_spec.rb +1 -1
  7. data/questions/sequential_check/section_1/part_2/lib/sentence.org +16 -6
  8. data/questions/sequential_check/section_1/part_3/lib/sentence.org +17 -7
  9. data/questions/sequential_check/section_2/part_1/lib/sentence.org +17 -6
  10. data/questions/sequential_check/section_2/part_2/lib/sentence.org +43 -0
  11. data/questions/sequential_check/section_2/part_3/lib/sentence.org +22 -5
  12. data/questions/sequential_check/section_2/part_3/spec/workplace_spec.rb +2 -2
  13. data/questions/sequential_check/section_3/part_1/lib/sentence.org +56 -0
  14. data/questions/sequential_check/section_3/part_3/lib/answer.rb +5 -0
  15. data/questions/sequential_check/section_3/part_3/lib/sentence.org +6 -0
  16. data/questions/sequential_check/section_3/part_3/lib/workplace.rb +2 -0
  17. data/questions/sequential_check/section_3/part_3/spec/spec_helper.rb +100 -0
  18. data/questions/sequential_check/section_3/part_3/spec/workplace_spec.rb +9 -0
  19. data/questions/sequential_check/section_4/part_1/lib/answer.rb +19 -8
  20. data/questions/sequential_check/section_4/part_1/lib/sentence.org +94 -11
  21. data/questions/sequential_check/section_4/part_1/lib/workplace.rb +17 -0
  22. data/questions/sequential_check/section_4/part_1/spec/workplace_spec.rb +5 -10
  23. data/questions/sequential_check/section_4/part_2/lib/answer.rb +9 -0
  24. data/questions/sequential_check/section_4/part_2/lib/sentence.org +85 -0
  25. data/questions/sequential_check/section_4/part_2/lib/workplace.rb +2 -0
  26. data/questions/sequential_check/section_4/part_2/spec/spec_helper.rb +100 -0
  27. data/questions/sequential_check/section_4/part_2/spec/workplace_spec.rb +14 -0
  28. data/questions/sequential_check/section_4/part_3/lib/answer.rb +13 -0
  29. data/questions/sequential_check/section_4/part_3/lib/sentence.org +88 -0
  30. data/questions/sequential_check/section_4/part_3/lib/workplace.rb +4 -0
  31. data/questions/sequential_check/section_4/part_3/spec/spec_helper.rb +100 -0
  32. data/questions/sequential_check/section_4/part_3/spec/workplace_spec.rb +29 -0
  33. data/questions/sequential_check/section_5/part_1/lib/answer.rb +3 -3
  34. data/questions/sequential_check/section_5/part_1/lib/sentence.org +60 -7
  35. data/questions/sequential_check/section_5/part_1/lib/workplace.rb +1 -0
  36. data/questions/sequential_check/section_5/part_1/spec/workplace_spec.rb +30 -9
  37. data/questions/sequential_check/section_5/part_2/lib/answer.rb +11 -2
  38. data/questions/sequential_check/section_5/part_2/lib/sentence.org +70 -6
  39. data/questions/sequential_check/section_5/part_2/lib/workplace.rb +9 -0
  40. data/questions/sequential_check/section_5/part_2/spec/workplace_spec.rb +18 -5
  41. data/questions/sequential_check/section_5/part_3/lib/answer.rb +17 -0
  42. data/questions/sequential_check/section_5/part_3/lib/sentence.org +74 -0
  43. data/questions/sequential_check/section_5/part_3/lib/workplace.rb +10 -0
  44. data/questions/sequential_check/section_5/part_3/spec/spec_helper.rb +100 -0
  45. data/questions/sequential_check/section_5/part_3/spec/workplace_spec.rb +22 -0
  46. data/questions/sequential_check/section_6/part_1/lib/answer.rb +7 -2
  47. data/questions/sequential_check/section_6/part_1/lib/sentence.org +13 -14
  48. data/questions/sequential_check/section_6/part_1/lib/workplace.rb +2 -0
  49. data/questions/sequential_check/section_6/part_1/spec/workplace_spec.rb +21 -9
  50. data/questions/sequential_check/section_6/part_2/lib/sentence.org +1 -0
  51. data/workshop/rubocop.yml +2 -0
  52. metadata +22 -2
@@ -0,0 +1,14 @@
1
+ require "open3"
2
+
3
+ RSpec.describe "ARGV-check" do
4
+ it 'given 9, return "smaller\n"' do
5
+ workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
6
+ stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb 9")
7
+ expect { puts stdout }.to output("smaller\n").to_stdout
8
+ end
9
+ it 'given 10, return "greater\n"' do
10
+ workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
11
+ stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb 10")
12
+ expect { puts stdout }.to output("greater\n").to_stdout
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ # section_4/part_3/answer.rb
2
+ # 4-3-case.rb
3
+ def gree()
4
+ country = STDIN.gets.chomp
5
+ case country
6
+ when 'Japan' then
7
+ puts 'こんにちは'
8
+ when 'USA' then
9
+ puts 'Hello'
10
+ else
11
+ print 'else'
12
+ end
13
+ end
@@ -0,0 +1,88 @@
1
+ -課題-
2
+ キーボードから入力された値を受け取って,その値によって出力結果を変更するプログラムを「case」を用いて作成せよ.
3
+ 受け取った値が「Japan」の場合はputsを用いて「こんにちは」を出力せよ.
4
+ 受け取った値が「USA」の場合はputsを用いて「Hello」を出力せよ.
5
+ それ以外の場合はprintを用いて「else」を出力せよ.
6
+
7
+ * 擬似変数
8
+ - 数ある変数の内の1つ.
9
+ - 疑似変数の値を変更することはできない.
10
+ - 今回はtrue,false,nilを紹介する.
11
+ - true, false...比較演算の結果や条件を表すメソッドの結果として多く用いられる.これらを変数の結果として格納することも可能.
12
+ - nil...オブジェクトが存在していないことを表す.
13
+
14
+ * 真偽値
15
+ - 真...falseとnilを除くオブジェクト全て.
16
+ - 偽...falseとnil
17
+
18
+ * 条件分岐
19
+ - 条件によってプログラムの挙動を変える際に用いるメソッド.
20
+ - 条件には値が「true」か「false」となる式を書くのが一般的.
21
+ - 「nil」を返すオブジェクトも条件に使用できる.
22
+
23
+ ** if
24
+ - 使用方法...if 条件 then 実行したい処理 end (if のメソッド内では「then」は省略可能.)
25
+ ex) if a>9 then
26
+ puts a
27
+ end
28
+
29
+ - 機能...条件が「true」の場合のみ,実行したい処理を行う.「else」と「elsif」を用いて,複雑な条件分岐を実行することができる.
30
+ ex) 使用方法の例を解説する.「『a』が9より大きい場合のみ,puts で『a』の値を表示する.」
31
+
32
+ *** elsifとelse
33
+ - 使用方法...if 条件1 then
34
+ 条件1で実行したい処理
35
+ elsif 条件2 then
36
+         条件2で実行したい処理
37
+ else
38
+ 条件1と条件2の両方に当てはまらない場合の処理
39
+ end
40
+
41
+ - 機能...条件を2つ以上使う場合は,条件2つ目から「if」ではなく「elsif」をつける.指定した条件の全てで「false」の場合の処理はelse内で記述する.
42
+ ** unless
43
+ - if は条件が「true」の場合に処理を実行するメソッドであるが,unless は条件が「false」の場合に処理を実行するメソッドである.
44
+ - 使用方法...基本的にはif メソッドの「if」を「unless」に書き換えるだけである.しかし,unless メソッドにはif メソッドの「elsif」に該当する機能は無い.
45
+ ex) unless a>9 then
46
+ puts 'aは9以下'
47
+ else
48
+ puts 'aは9より大きい'
49
+ end
50
+
51
+ - 機能...条件が「false」の場合のみ,実行したい処理を行う.
52
+ ex) 使用方法の例を解説する.「『a』が『9より大きい』が『false』の場合は『aは9以下』,それ以外は『aは9より大きい』と表示する」
53
+
54
+ ** case
55
+ - 使用方法...case 比較したいオブジェクト
56
+ when 値1 then
57
+ 文1
58
+ when 値2 then
59
+ 文2
60
+ else
61
+ 文3
62
+ end
63
+
64
+ -機能...比較したいオブジェクトの値が値1の場合は文1が実行される.値2も同様である.elseはどの値にも当てはまらない場合に実行される.
65
+
66
+ * 結果が「true」や「false」となる式.(比較演算子)
67
+ - 条件を満たさない場合は「false」,それ以外は「true」を返す.
68
+ - ここでは関係演算子と論理演算子の2つを紹介する.
69
+ *** 関係演算子
70
+ - 左辺と右辺のオブジェクトが持つ数値や文字列を比較し,等しいか等しく無いか,また大きいか小さいかなどを比較することができる,
71
+ - 今回は2つのオブジェクト「a」と「b」の関係で解説していく.
72
+
73
+ - (具体例) a == b (意味)「b」が「a」に等しい.
74
+ - (具体例) a != b (意味)「b」が「a」に等くない.
75
+ - (具体例) a > b (意味)「a」は「b」より大きい.
76
+ - (具体例) a >= b (意味)「a」は「b」以上.
77
+ - (具体例) a < b (意味)「a」は「b」より小さい.
78
+ - (具体例) a <= b (意味)「a」は「b」以下.
79
+
80
+ - 「a」と「b」を関係演算子を用いて比較した結果として,関係演算子の持つ意味を満たす場合は「true」,満たさない場合は「false」を返す.
81
+
82
+ *** 論理演算子
83
+ - 複数の関係演算子を満たす場合の条件を書くときに,よく使われる条件の書き方.
84
+ - 今回は2つの条件式「A」と「B」の関係で解説していく.
85
+ - (具体例) A && B (意味)「A」と「B」が共に「true」の場合は「true」を返す.
86
+ - (具体例) A || B (意味)「A」と「B」のどちらか一方でも「true」の場合は「true」を返す.
87
+ - (具体例) !A (意味)「A」が「true」の場合は「false」,「false」の場合は「true」を返す.
88
+
@@ -0,0 +1,4 @@
1
+ # section_4/part_3/workplace.rb
2
+ def gree()
3
+ # add your answer
4
+ end
@@ -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,29 @@
1
+ require "open3"
2
+ require "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
3
+
4
+ filename = "#{ENV['HOME']}/.ruby_learner/workshop/lib/workplace.rb"
5
+ check_method = false
6
+
7
+ RSpec.describe "STDIN-check" do
8
+ it 'check case-method, return boolean' do
9
+ File.open(filename, "r") do |file|
10
+ file.each_line do |line|
11
+ check_method = true if line.include?('case')
12
+ end
13
+ end
14
+ puts "Message: you don't use case-methods." if !check_method
15
+ expect( check_method ).to eq(true)
16
+ end
17
+ it 'given Japan, return "こんにちは\n"' do
18
+ allow(STDIN).to receive(:gets) { "Japan\n" }
19
+ expect { gree() }.to output("こんにちは\n").to_stdout
20
+ end
21
+ it 'given USA, return "Hello\n"' do
22
+ allow(STDIN).to receive(:gets) { "USA\n" }
23
+ expect { gree() }.to output("Hello\n").to_stdout
24
+ end
25
+ it 'given Italy, return "else\n"' do
26
+ allow(STDIN).to receive(:gets) { "Italy\n" }
27
+ expect { gree() }.to output("else").to_stdout
28
+ end
29
+ end
@@ -1,8 +1,8 @@
1
1
  # section_5/part_1/answer.rb
2
2
  # 5-1-while.rb
3
- n = ARGV[0].to_i
3
+ max = 15
4
4
  i = 1
5
- while i <= n
6
- print i, "\n"
5
+ while i <= max
6
+ puts i
7
7
  i += 1
8
8
  end
@@ -1,10 +1,63 @@
1
- 1つの数値をnとして受け取り、1からnまでの数を順番に表示するプログラムを作成せよ。
1
+ -課題-
2
+ 1から与えられた変数(max)までの数を順番にputsで表示するプログラムを9行以内で作成せよ。ただしwhileでの繰り返しを行うこと.
2
3
 
3
- - while文
4
+ * 繰り返し処理
5
+ - 同じこと,または同じようなことを何度か繰り返したい場合に使うメソッドの紹介.
6
+ - 以下のメソッドのeach以外の「do」は省略可能.
7
+ ** while
8
+ - 使用方法...while 条件 do
9
+ 繰り返したい処理
10
+ end
11
+ - 機能...条件が「true」である間,繰り返したい処理を何度も実行する.
4
12
 
5
- while 繰り返し続ける条件
6
- 繰り返したい処理
7
- end
13
+ ** until
14
+ - 使用方法...until 条件 do
15
+ 繰り返したい処理
16
+      end
17
+ - 機能...条件が「false」である間,繰り返したい処理を何度も実行する.
18
+ ** times
19
+ - 使用方法...繰り返す回数.times do
20
+ 繰り返したい処理
21
+ end
22
+ - 機能...繰り返す回数だけ,繰り返したい処理を行う.
8
23
 
9
- 繰り返しを行うための基本的な構文。
10
- 条件が成り立つ限り、処理を繰り返す。
24
+ ** for
25
+ - 使用方法...for 変数 in オブジェクト do
26
+ 繰り返したい処理
27
+ end
28
+ - 機能...指定したオブジェクトの内容によって,変数に値が入り,オブジェクトの終わりまで繰り返しを行う.
29
+ - 使用できるオブジェクトの一例を以下に記す.
30
+
31
+ *** 範囲オブジェクト
32
+ - 範囲オブジェクトは指定した最初の値と最後の値の範囲を表すオブジェクトである.
33
+ - 「..」演算子...「..」の左辺に最初の値を指定し右辺に最後の値を指定することでその範囲を表す.
34
+ ex) 「2..7」の場合は2から7の範囲を表す.
35
+
36
+ - 「...」演算子...「...」は「..」と同じ使い方ができる.ただし,範囲の最後は右辺の値は含めない.
37
+ ex) 「2..7」の場合は2から6の範囲を表す.
38
+ *** 配列
39
+ - 配列をforのオブジェクトにしてすると,配列の要素の数だけ繰り返しを行う.また,forの変数には繰り返しの回数と連動して配列の要素が順に代入される.
40
+ ex) for item in ['Hello', 'Ruby.']
41
+ puts item
42
+ end
43
+
44
+   出力結果
45
+ >Hello
46
+ >Ruby.
47
+   
48
+ ** each
49
+ - 使用方法...オブジェクト.each do |変数|
50
+       繰り返したい処理
51
+ end
52
+ - 機能...指定したオブジェクトの内容によって,変数に値が入り,オブジェクトの終わりまで繰り返しを行う.
53
+ - forで使用できるオブジェクトはeachでも使用可能.
54
+
55
+ ** loop
56
+ - 使用方法...loop do
57
+ 繰り返したい処理
58
+ end
59
+ - 機能...特別な制御を用いて繰り返しを行う.
60
+ *** 特別な制御
61
+ - break...繰り返しを中断し,繰り返しの中から抜ける.
62
+ - next...次の回の繰り返しに処理を移す.
63
+ - redo...同じ条件で繰り返しをやり直す.(ほとんど使われていない.)
@@ -1 +1,2 @@
1
1
  # section_5/part_1/workplace.rb
2
+ max = 15
@@ -1,14 +1,35 @@
1
1
  require "open3"
2
2
 
3
- RSpec.describe "ARGV-check" do
4
- it 'given 3, return "1\n2\n3\n"' do
5
- workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
6
- stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb 3")
7
- expect { puts stdout }.to output("1\n2\n3\n").to_stdout
3
+ is_while_method = false
4
+ last_lineno = 0
5
+ is_file_size = false
6
+ workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
7
+ filename = "#{workshop}/lib/workplace.rb"
8
+
9
+ RSpec.describe "while-check" do
10
+ it 'check while-method, return boolean' do
11
+ File.open(filename, "r") do |file|
12
+ file.each_line do |line|
13
+ is_while_method = true if line.include?('while')
14
+ last_lineno = file.lineno
15
+ end
16
+ end
17
+ puts "ErrorMessage: you don't use while-methods." if !is_while_method
18
+ expect( is_while_method ).to eq(true)
19
+ end
20
+
21
+ it 'check file_line_count, return boolean' do
22
+ if last_lineno > 9
23
+ is_file_size = false
24
+ puts "ErrorMessage: you should make file of 9 lines of less."
25
+ else
26
+ is_file_size = true
27
+ end
28
+ expect( is_file_size ).to eq(true)
8
29
  end
9
- it 'given 5, return "1\n2\n3\n4\n5\n"' do
10
- workshop = "#{ENV['HOME']}/.ruby_learner/workshop"
11
- stdout, stderr, status = Open3.capture3("ruby #{workshop}/lib/workplace.rb 5")
12
- expect { puts stdout }.to output("1\n2\n3\n4\n5\n").to_stdout
30
+
31
+ it 'return "1\n2\n..15\n"' do
32
+ stdout, stderr, status = Open3.capture3("ruby #{filename}")
33
+ expect { puts stdout }.to output("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n").to_stdout
13
34
  end
14
35
  end
@@ -1,5 +1,14 @@
1
1
  # section_5/part_2/answer.rb
2
2
  # 5-2-times.rb
3
- 5.times do
4
- print "All work and no play makes Jack a dull boy.\n"
3
+ names = %w[tanaka nakamura sasaki suzuki]
4
+ index = 0
5
+
6
+ names.size.times do
7
+ puts names[index]
8
+ index += 1
9
+ end
10
+
11
+ # add your code.
12
+ names.each do |name|
13
+ puts name
5
14
  end
@@ -1,10 +1,74 @@
1
- 「All work and no play makes Jack a dull boy.」と5行表示するプログラムを作成せよ。
1
+ -課題-
2
+ timesを用いて配列の要素を呼び出すプログラムが用意される.
3
+ その下に,新しく同じ処理を行うプログラムを追加せよ.
4
+ ただし,eachを用いること.
2
5
 
6
+ ヒント: namesの%w[]表記に関してはテキスト参照せよ.
3
7
 
4
- - timesメソッド
8
+ * %w[]表記
9
+ - 配列の要素が全て文字列の場合,文字列ごとのシングルクオーテーション(')が必要ないこの表記を用いる.
10
+ ex) names1 = ['tanaka', 'nakamura', 'sasaki']
11
+ namse2 = %w[tanaka nakamura sasaki]
12
+ namse1とnames2は同様の配列である.
5
13
 
6
- 繰り返す回数.times do
7
- 繰り返したい処理
8
- end
14
+ * 繰り返し処理
15
+ - 同じこと,または同じようなことを何度か繰り返したい場合に使うメソッドの紹介.
16
+ - 以下のメソッドのeach以外の「do」は省略可能.
17
+ ** while
18
+ - 使用方法...while 条件 do
19
+ 繰り返したい処理
20
+ end
21
+ - 機能...条件が「true」である間,繰り返したい処理を何度も実行する.
22
+
23
+ ** until
24
+ - 使用方法...until 条件 do
25
+ 繰り返したい処理
26
+      end
27
+ - 機能...条件が「false」である間,繰り返したい処理を何度も実行する.
28
+ ** times
29
+ - 使用方法...繰り返す回数.times do
30
+ 繰り返したい処理
31
+ end
32
+ - 機能...繰り返す回数だけ,繰り返したい処理を行う.
33
+
34
+ ** for
35
+ - 使用方法...for 変数 in オブジェクト do
36
+ 繰り返したい処理
37
+ end
38
+ - 機能...指定したオブジェクトの内容によって,変数に値が入り,オブジェクトの終わりまで繰り返しを行う.
39
+ - 使用できるオブジェクトの一例を以下に記す.
40
+
41
+ *** 範囲オブジェクト
42
+ - 範囲オブジェクトは指定した最初の値と最後の値の範囲を表すオブジェクトである.
43
+ - 「..」演算子...「..」の左辺に最初の値を指定し右辺に最後の値を指定することでその範囲を表す.
44
+ ex) 「2..7」の場合は2から7の範囲を表す.
45
+
46
+ - 「...」演算子...「...」は「..」と同じ使い方ができる.ただし,範囲の最後は右辺の値は含めない.
47
+ ex) 「2..7」の場合は2から6の範囲を表す.
48
+ *** 配列
49
+ - 配列をforのオブジェクトにしてすると,配列の要素の数だけ繰り返しを行う.また,forの変数には繰り返しの回数と連動して配列の要素が順に代入される.
50
+ ex) for item in ['Hello', 'Ruby.']
51
+ puts item
52
+ end
53
+
54
+   出力結果
55
+ >Hello
56
+ >Ruby.
57
+   
58
+ ** each
59
+ - 使用方法...オブジェクト.each do |変数|
60
+       繰り返したい処理
61
+ end
62
+ - 機能...指定したオブジェクトの内容によって,変数に値が入り,オブジェクトの終わりまで繰り返しを行う.
63
+ - forで使用できるオブジェクトはeachでも使用可能.
64
+
65
+ ** loop
66
+ - 使用方法...loop do
67
+ 繰り返したい処理
68
+ end
69
+ - 機能...特別な制御を用いて繰り返しを行う.
70
+ *** 特別な制御
71
+ - break...繰り返しを中断し,繰り返しの中から抜ける.
72
+ - next...次の回の繰り返しに処理を移す.
73
+ - redo...同じ条件で繰り返しをやり直す.(ほとんど使われていない.)
9
74
 
10
- 繰り返す回数が決まっている場合は、timesメソッドを使ってシンプルにできる。