diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2133eb0 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gem "standard" +gem "tty-markdown" +gem "timeout" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..cc42973 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,65 @@ +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + json (2.6.3) + kramdown (2.4.0) + rexml + language_server-protocol (3.17.0.3) + parallel (1.22.1) + parser (3.2.1.0) + ast (~> 2.4.1) + pastel (0.8.0) + tty-color (~> 0.5) + rainbow (3.1.1) + regexp_parser (2.7.0) + rexml (3.2.5) + rouge (3.30.0) + rubocop (1.44.1) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.24.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.26.0) + parser (>= 3.2.1.0) + rubocop-performance (1.15.2) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + ruby-progressbar (1.11.0) + standard (1.24.3) + language_server-protocol (~> 3.17.0.2) + rubocop (= 1.44.1) + rubocop-performance (= 1.15.2) + strings (0.2.1) + strings-ansi (~> 0.2) + unicode-display_width (>= 1.5, < 3.0) + unicode_utils (~> 1.4) + strings-ansi (0.2.0) + timeout (0.3.2) + tty-color (0.6.0) + tty-markdown (0.7.1) + kramdown (>= 1.16.2, < 3.0) + pastel (~> 0.8) + rouge (~> 3.14) + strings (~> 0.2.0) + tty-color (~> 0.5) + tty-screen (~> 0.8) + tty-screen (0.8.1) + unicode-display_width (2.4.2) + unicode_utils (1.4.0) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + standard + timeout + tty-markdown + +BUNDLED WITH + 2.3.26 diff --git a/README.md b/README.md index 1bcab05..ecb6c38 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,14 @@ Sample questions for [Ruby Association Certified Ruby Programmer Examination ver * [Sample Questions for Silver](silver.md) ([Japanese](silver_ja.md)) * [Sample Questions for Gold](gold.md) ([Japanese](gold_ja.md)) + +# Run sample exam + +* Silver `ruby test.rb` or `ruby test.rb silver en` +* Gold `ruby test.rb gold en` + +Japanese + +* Silver `ruby test.rb silver ja` +* Gold `ruby test.rb gold ja` + diff --git a/gold.md b/gold.md index cb45dff..8cb837d 100644 --- a/gold.md +++ b/gold.md @@ -4,7 +4,7 @@ **Q1. Assume that the following code must have the stated execution result:** -``` +```ruby class Stack def initialize @contents = [] @@ -25,7 +25,8 @@ p stack.pop **Which option can be inserted into `__(1)__`? (Choose two.)** *A:* -``` + +```ruby [:push, :pop].each do |name| define_method(name) do |*args| @contents.send(name, *args) @@ -34,7 +35,8 @@ end ``` *B:* -``` + +```ruby for name in [:push, :pop] define_method(name) do |*args| @contents.send(name, *args) @@ -43,7 +45,8 @@ end ``` *C:* -``` + +```ruby [:push, :pop].each do |name| instance_eval(<<-EOF) def #{name}(*args) @@ -54,7 +57,8 @@ end ``` *D:* -``` + +```ruby [:push, :pop].each do |name| class_eval(<<-EOF) def #{name}(*args) @@ -68,7 +72,7 @@ end **Q2. Given the following code:** -``` +```ruby module I end @@ -97,7 +101,7 @@ p C.ancestors **Q3. Assume that the following code must have the stated execution result:** -``` +```ruby x = __(1)__ p x + 1 @@ -119,7 +123,7 @@ p x + 1 **Q4. Assume that the following code must have the stated execution result:** -``` +```ruby p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ [Execution Result] @@ -140,7 +144,7 @@ p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ **Q5. Assume that the following code must have the stated execution result:** -``` +```ruby def round(n, __(1)__) n.round(__(1)__) end @@ -165,7 +169,7 @@ p round(2.5, half: :even) **Q6. Given the following code:** -``` +```ruby class A def foo self.bar @@ -207,7 +211,7 @@ An exception is raised **Q7. Assume that the following code must have the stated execution result.** -``` +```ruby class Greeter class << self def hello @@ -236,7 +240,7 @@ Hello there! **Q8: Assume that the following code must have the stated execution result.** -``` +```ruby __(1)__ p multiply_by(4) { 2 + 3 } @@ -249,7 +253,7 @@ p multiply_by(4) { 2 + 3 } *A:* -``` +```ruby def multiply_by(n, &block) n * block.call end @@ -257,7 +261,7 @@ end *B:* -``` +```ruby def multiply_by(n, &block) n * block end @@ -265,7 +269,7 @@ end *C:* -``` +```ruby def multiply_by(n) n * yield end @@ -273,7 +277,7 @@ end *D:* -``` +```ruby def multiply_by(n) n * yield.call end @@ -283,7 +287,7 @@ end **Q9: Assume that the following code must have the stated execution result.** -``` +```ruby __(1)__ p sum { |e| e << 1 << 5 << 7 } @@ -296,7 +300,7 @@ p sum { |e| e << 1 << 5 << 7 } *A:* -``` +```ruby def sum(&block) array = [] @@ -307,7 +311,8 @@ end ``` *B:* -``` + +```ruby def sum(&block) array = [] @@ -318,7 +323,8 @@ end ``` *C:* -``` + +```ruby def sum array = [] @@ -329,7 +335,8 @@ end ``` *D:* -``` + +```ruby def sum array = [] @@ -343,7 +350,7 @@ end **Q10. Given the following code:** -``` +```ruby class A @@x = 1 end @@ -378,7 +385,7 @@ p B.x **Q11. Assume that the following code must have the stated execution result:** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = Proc.new { words.pop } 3.times{ puts __(1)__ } @@ -405,7 +412,7 @@ apple **Q12. Assume that the following code must have the stated execution result:** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = __(1)__{ words.pop } 3.times{ puts pop.call } @@ -430,7 +437,7 @@ apple **Q13. Assume that the following code must have the stated execution result.** -``` +```ruby add = __(1)__ puts add.call("hello") @@ -452,7 +459,7 @@ HELLO **Q14. Assume that the following code must have the stated execution result:** -``` +```ruby __(1)__ x + y end @@ -477,7 +484,7 @@ p add(1, 2) **Q15. Given the following code:** -``` +```ruby def reader_method(s) <<~EOF def #{s} @@ -492,21 +499,24 @@ print reader_method("foo") **Which option corresponds to the execution result? (Choose one.)** *A:* -``` + +```ruby def foo @foo end ``` *B:* -``` + +```ruby def foo @foo end ``` *C:* -``` + +```ruby def foo @foo end @@ -519,7 +529,7 @@ A syntax error occurs. **Q16. Assume that the following code must have the stated execution result:** -``` +```ruby obj = Object.new def obj.hello @@ -548,7 +558,7 @@ Hi! **Q17. Assume that the following code must have the stated execution result.** -``` +```ruby class ShoppingList def initialize @items = [] @@ -591,7 +601,7 @@ puts list **Q18. Assume that the following code must have the stated execution result.** -``` +```ruby class ShoppingList def initialize @items = [] @@ -633,7 +643,7 @@ ShoppingList: **Q19. Assume that the following code must have the stated execution result.** -``` +```ruby p __(1)__.flat_map {|z| (1..z).flat_map {|x| (x..z).select {|y| @@ -662,7 +672,7 @@ p __(1)__.flat_map {|z| **Q20. Assume that the following code must have the stated execution result.** -``` +```ruby ary = ["foo", "bar", nil, "baz"] p ary.__(1)__ { |i| @@ -687,7 +697,7 @@ p ary.__(1)__ { |i| **Q21. Given the following code:** -``` +```ruby a, b, *c = ["apple", "banana", "carrot", "daikon"] p c @@ -707,13 +717,14 @@ p c **Q22: Which code produces the following execution result? (Choose one.)** -``` +```ruby [Execution Result] [["apple", "banana", "carrot"]] ``` *A:* -``` + +```ruby def fx(*args) p(args) end @@ -721,7 +732,8 @@ fx(*["apple", "banana", "carrot"]) ``` *B:* -``` + +```ruby def fx(*args) p(args) end @@ -729,7 +741,8 @@ fx(["apple", "banana", "carrot"]) ``` *C:* -``` + +```ruby def fx(*args) p(args) end @@ -737,7 +750,8 @@ fx("apple", "banana", "carrot") ``` *D:* -``` + +```ruby def fx(*args) p(*args) end @@ -748,7 +762,7 @@ fx(["apple", "banana", "carrot"]) **Q23. Assume that the following code must have the stated execution result.** -``` +```ruby p ["foo", "bar", "baz"].map { __(1)__.upcase } [Execution Result] @@ -769,7 +783,7 @@ p ["foo", "bar", "baz"].map { __(1)__.upcase } **Q24. Given the following code:** -``` +```ruby def fx(a:, b: "apple") p a p b @@ -781,13 +795,15 @@ fx(b: "banana") **Which option corresponds to the execution result?** *A:* -``` + +```ruby nil apple ``` *B:* -``` + +```ruby nil banana ``` @@ -802,7 +818,7 @@ An exception is raised **Q25. Assume that the following code must have the stated execution result.** -``` +```ruby def add(x:, y:, **params) z = x + y @@ -831,7 +847,7 @@ __(1)__ **Q26: Assume that the following code must have the stated execution result.** -``` +```ruby class Speaker @message = "Hello!" @@ -864,7 +880,7 @@ Hello! **Q27. Given the following code:** -``` +```ruby class Speaker @message = "Hello!" @@ -892,7 +908,7 @@ end **Q28. Assume that the following code must have the stated execution result.** -``` +```ruby def x puts "x" end @@ -935,7 +951,7 @@ done! **Q29. Assume that the following code must have the stated execution result.** -``` +```ruby letters = catch(:done) do ("a".."z").each do |a| ("a".."z").each do |b| @@ -967,7 +983,7 @@ abc **Q30. Assume that the following code must have the stated execution result.** -``` +```ruby begin __(1)__ rescue @@ -992,7 +1008,7 @@ OK **Q31. Given the following code:** -``` +```ruby AnError = Class.new(Exception) begin @@ -1011,27 +1027,32 @@ end **Which option corresponds to the execution result? (Choose one.)** *A:* -``` + +```ruby Bare rescue ``` *B:* -``` + +```ruby StandardError rescue ``` *C:* -``` + +```ruby AnError rescue ``` *D:* -``` + +```ruby Exception rescue ``` *E:* -``` + +```ruby AnError rescue Exception rescue ``` @@ -1040,7 +1061,7 @@ Exception rescue **Q32. Given the following code:** -``` +```ruby AnError = Class.new(Exception) begin @@ -1059,27 +1080,32 @@ end **Which option corresponds to the execution result? (Choose one.)** *A:* -``` + +```ruby Bare rescue ``` *B:* -``` + +```ruby StandardError rescue ``` *C:* -``` + +```ruby AnError rescue ``` *D:* -``` + +```ruby Exception rescue ``` *E:* -``` + +```ruby Exception rescue AnError rescue ``` @@ -1088,7 +1114,7 @@ AnError rescue **Q33. Given the following code:** -``` +```ruby begin "hello".world rescue => ex @@ -1112,7 +1138,7 @@ end **Q34. Given the following code:** -``` +```ruby CustomError = Class.new(StandardError) def boom @@ -1144,7 +1170,7 @@ end **Q35. Given the following code:** -``` +```ruby def greeting "hello" ensure @@ -1160,27 +1186,27 @@ puts greeting *A:* -``` +```ruby hello ``` *B:* -``` +```ruby hi ``` *C:* -``` +```ruby Ensure called! hello ``` *D:* -``` +```ruby Ensure called! hi ``` @@ -1189,7 +1215,7 @@ hi **Q36. Given the following code:** -``` +```ruby class Identity def self.this_object self @@ -1214,25 +1240,28 @@ p c == d *A:* -``` +```ruby true true ``` *B:* -``` + +```ruby true false ``` *C:* -``` + +```ruby false true ``` *D:* -``` + +```ruby false false ``` @@ -1241,7 +1270,7 @@ false **Q37. Given the following code:** -``` +```ruby class Identity def self.this_object self.class @@ -1259,25 +1288,29 @@ p Identity.new.this_object.class **Which option corresponds to the execution result? (Choose one.)** *A:* -``` + +```ruby Identity Identity ``` *B:* -``` + +```ruby Class Identity ``` *C:* -``` + +```ruby Object Identity ``` *D:* -``` + +```ruby Class Object ``` @@ -1286,7 +1319,7 @@ Object **Q38. Given the following code:** -``` +```ruby module Mixin def this_object self @@ -1327,7 +1360,7 @@ p Identity.new.this_object.class **Q40. Given the following code:** -``` +```ruby module Mixin def self.greet puts "Hello World!" @@ -1353,7 +1386,7 @@ end **Q41. Assume that the following code must have the stated execution result.** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1384,7 +1417,7 @@ Hello World! **Q42. Assume that the following code must have the stated execution result.** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1415,7 +1448,7 @@ Hello World! **Q43. Assume that the following code must have the stated execution result.** -``` +```ruby class BaseClass private @@ -1439,19 +1472,19 @@ Hello World! *A:* -``` +```ruby public :greet ``` *B:* -``` +```ruby protected :greet ``` *C:* -``` +```ruby def greet super end @@ -1459,7 +1492,7 @@ end *D:* -``` +```ruby alias_method :original_greet, :greet def greet @@ -1471,7 +1504,7 @@ end **Q44. Assume that the following code must have the stated execution result:** -``` +```ruby h = [1, 2, 3] case h __(1)__ [x, y] @@ -1498,7 +1531,7 @@ end **Q45. Assume that the following code must have the stated execution result:** -``` +```ruby class Alphabet include Enumerable @@ -1539,7 +1572,7 @@ p set.select { |e| e > "W" } **Q46. Given the following code:** -``` +```ruby class TShirt SIZES = [:xs, :s, :m, :l, :xl, :xxl] @@ -1569,7 +1602,7 @@ p medium >= large *A:* -``` +```ruby true false true @@ -1578,7 +1611,7 @@ true ``` *B:* -``` +```ruby false true true @@ -1587,7 +1620,7 @@ false ``` *C:* -``` +```ruby false false false @@ -1596,7 +1629,7 @@ true ``` *D:* -``` +```ruby false false false @@ -1611,7 +1644,7 @@ An exception is raised. **Q47. Given the following code:** -``` +```ruby require "date" date = Date.new(2000, 2, 24) @@ -1623,25 +1656,25 @@ puts(date >> 12) **Which option corresponds to the execution result? (Choose one.)** *A:* -``` +```ruby 2000-02-12 2000-03-07 ``` *B:* -``` +```ruby 2000-03-07 2000-02-12 ``` *C:* -``` +```ruby 1999-02-24 2001-02-24 ``` *D:* -``` +```ruby 1988-02-24 2012-02-24 ``` @@ -1650,7 +1683,7 @@ puts(date >> 12) **Q48. Assume that the following code must have the stated execution result:** -``` +```ruby require "time" t = Time.__(1)__("00000024021993", "%S%M%H%d%m%Y") @@ -1674,7 +1707,7 @@ puts t.iso8601 **Q49. Assume that the following code must have the stated execution result:** -``` +```ruby require "singleton" class Foo @@ -1703,7 +1736,7 @@ true **Q50. Assume that the following code must have the stated execution result:** -``` +```ruby require 'forwardable' class List diff --git a/gold_ja.md b/gold_ja.md index 6364923..9c80e61 100644 --- a/gold_ja.md +++ b/gold_ja.md @@ -4,7 +4,7 @@ **Q1. 以下の実行結果を出力するコードがあります。** -``` +```ruby class Stack def initialize @contents = [] @@ -25,7 +25,8 @@ p stack.pop **__(1)__に入る適切な記述を選択してください。(2つ選択)** *A:* -``` + +```ruby [:push, :pop].each do |name| define_method(name) do |*args| @contents.send(name, *args) @@ -34,7 +35,8 @@ end ``` *B:* -``` + +```ruby for name in [:push, :pop] define_method(name) do |*args| @contents.send(name, *args) @@ -43,7 +45,8 @@ end ``` *C:* -``` + +```ruby [:push, :pop].each do |name| instance_eval(<<-EOF) def #{name}(*args) @@ -54,7 +57,8 @@ end ``` *D:* -``` + +```ruby [:push, :pop].each do |name| class_eval(<<-EOF) def #{name}(*args) @@ -68,7 +72,7 @@ end **Q2. 以下のコードがあります。** -``` +```ruby module I end @@ -97,7 +101,7 @@ p C.ancestors **Q3. 以下の実行結果を出力するコードがあります。** -``` +```ruby x = __(1)__ p x + 1 @@ -119,7 +123,7 @@ p x + 1 **Q4. 以下の実行結果を出力するコードがあります。** -``` +```ruby p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ [実行結果] @@ -140,7 +144,7 @@ p ("aaaaaa".."zzzzzz").lazy.select { |e| e.end_with?("f") }.__(1)__ **Q5. 以下の実行結果を出力するコードがあります。** -``` +```ruby def round(n, __(1)__) n.round(__(1)__) end @@ -165,7 +169,7 @@ p round(2.5, half: :even) **Q6. 以下のコードがあります。** -``` +```ruby class A def foo self.bar @@ -188,12 +192,14 @@ puts A.new.foo **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby baz ``` *B:* -``` + +```ruby quux ``` @@ -207,7 +213,7 @@ quux **Q7. 以下の実行結果を出力するコードがあります。** -``` +```ruby class Greeter class << self def hello @@ -236,7 +242,7 @@ Hello there! **Q8: 以下の実行結果を出力するコードがあります。** -``` +```ruby __(1)__ p multiply_by(4) { 2 + 3 } @@ -249,7 +255,7 @@ p multiply_by(4) { 2 + 3 } *A:* -``` +```ruby def multiply_by(n, &block) n * block.call end @@ -257,7 +263,7 @@ end *B:* -``` +```ruby def multiply_by(n, &block) n * block end @@ -265,7 +271,7 @@ end *C:* -``` +```ruby def multiply_by(n) n * yield end @@ -273,7 +279,7 @@ end *D:* -``` +```ruby def multiply_by(n) n * yield.call end @@ -283,7 +289,7 @@ end **Q9: 以下の実行結果を出力するコードがあります。** -``` +```ruby __(1)__ p sum { |e| e << 1 << 5 << 7 } @@ -296,7 +302,7 @@ p sum { |e| e << 1 << 5 << 7 } *A:* -``` +```ruby def sum(&block) array = [] @@ -307,7 +313,8 @@ end ``` *B:* -``` + +```ruby def sum(&block) array = [] @@ -318,7 +325,8 @@ end ``` *C:* -``` + +```ruby def sum array = [] @@ -329,7 +337,8 @@ end ``` *D:* -``` + +```ruby def sum array = [] @@ -343,7 +352,7 @@ end **Q10. 以下のコードがあります。** -``` +```ruby class A @@x = 1 end @@ -378,7 +387,7 @@ p B.x **Q11. 以下の実行結果を出力するコードがあります。** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = Proc.new { words.pop } 3.times{ puts __(1)__ } @@ -405,7 +414,7 @@ apple **Q12. 以下の実行結果を出力するコードがあります。** -``` +```ruby words = ["apple", "banana", "cabbage"] pop = __(1)__{ words.pop } 3.times{ puts pop.call } @@ -430,7 +439,7 @@ apple **Q13. 以下の実行結果を出力するコードがあります。** -``` +```ruby add = __(1)__ puts add.call("hello") @@ -452,7 +461,7 @@ HELLO **Q14. 以下の実行結果を出力するコードがあります。** -``` +```ruby __(1)__ x + y end @@ -477,7 +486,7 @@ p add(1, 2) **Q15. 以下のコードがあります。** -``` +```ruby def reader_method(s) <<~EOF def #{s} @@ -492,21 +501,24 @@ print reader_method("foo") **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby def foo @foo end ``` *B:* -``` + +```ruby def foo @foo end ``` *C:* -``` + +```ruby def foo @foo end @@ -519,7 +531,7 @@ end **Q16. 以下の実行結果を出力するコードがあります。** -``` +```ruby obj = Object.new def obj.hello @@ -548,7 +560,7 @@ Hi! **Q17. 以下の実行結果を出力するコードがあります。** -``` +```ruby class ShoppingList def initialize @items = [] @@ -591,7 +603,7 @@ puts list **Q18. 以下の実行結果を出力するコードがあります。** -``` +```ruby class ShoppingList def initialize @items = [] @@ -633,7 +645,7 @@ ShoppingList: **Q19. 以下の実行結果を出力するコードがあります。** -``` +```ruby p __(1)__.flat_map {|z| (1..z).flat_map {|x| (x..z).select {|y| @@ -662,7 +674,7 @@ p __(1)__.flat_map {|z| **Q20. 以下の実行結果を出力するコードがあります。** -``` +```ruby ary = ["foo", "bar", nil, "baz"] p ary.__(1)__ { |i| @@ -687,7 +699,7 @@ p ary.__(1)__ { |i| **Q21. 以下のコードがあります。** -``` +```ruby a, b, *c = ["apple", "banana", "carrot", "daikon"] p c @@ -707,13 +719,14 @@ p c **Q22: 以下の実行結果を出力するコードを選択してください。(1つ選択)** -``` +```ruby [実行結果] [["apple", "banana", "carrot"]] ``` *A:* -``` + +```ruby def fx(*args) p(args) end @@ -721,7 +734,8 @@ fx(*["apple", "banana", "carrot"]) ``` *B:* -``` + +```ruby def fx(*args) p(args) end @@ -729,7 +743,8 @@ fx(["apple", "banana", "carrot"]) ``` *C:* -``` + +```ruby def fx(*args) p(args) end @@ -737,7 +752,8 @@ fx("apple", "banana", "carrot") ``` *D:* -``` + +```ruby def fx(*args) p(*args) end @@ -748,7 +764,7 @@ fx(["apple", "banana", "carrot"]) **Q23. 以下の実行結果を出力するコードがあります。** -``` +```ruby p ["foo", "bar", "baz"].map { __(1)__.upcase } [実行結果] @@ -769,7 +785,7 @@ p ["foo", "bar", "baz"].map { __(1)__.upcase } **Q24. 以下のコードがあります。** -``` +```ruby def fx(a:, b: "apple") p a p b @@ -781,13 +797,15 @@ fx(b: "banana") **実行結果として正しいものを選択してください。** *A:* -``` + +```ruby nil apple ``` *B:* -``` + +```ruby nil banana ``` @@ -802,7 +820,7 @@ banana **Q25. 以下の実行結果を出力するコードがあります。** -``` +```ruby def add(x:, y:, **params) z = x + y @@ -831,7 +849,7 @@ __(1)__ **Q26: 以下の実行結果を出力するコードがあります。** -``` +```ruby class Speaker @message = "Hello!" @@ -864,7 +882,7 @@ Hello! **Q27. 以下のコードがあります。** -``` +```ruby class Speaker @message = "Hello!" @@ -892,7 +910,7 @@ end **Q28. 以下の実行結果を出力するコードがあります。** -``` +```ruby def x puts "x" end @@ -935,7 +953,7 @@ done! **Q29. 以下の実行結果を出力するコードがあります。** -``` +```ruby letters = catch(:done) do ("a".."z").each do |a| ("a".."z").each do |b| @@ -967,7 +985,7 @@ abc **Q30. 以下の実行結果を出力するコードがあります。** -``` +```ruby begin __(1)__ rescue @@ -992,7 +1010,7 @@ OK **Q31. 以下のコードがあります。** -``` +```ruby AnError = Class.new(Exception) begin @@ -1011,27 +1029,32 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby Bare rescue ``` *B:* -``` + +```ruby StandardError rescue ``` *C:* -``` + +```ruby AnError rescue ``` *D:* -``` + +```ruby Exception rescue ``` *E:* -``` + +```ruby AnError rescue Exception rescue ``` @@ -1040,7 +1063,7 @@ Exception rescue **Q32. 以下のコードがあります。** -``` +```ruby AnError = Class.new(Exception) begin @@ -1059,27 +1082,32 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby Bare rescue ``` *B:* -``` + +```ruby StandardError rescue ``` *C:* -``` + +```ruby AnError rescue ``` *D:* -``` + +```ruby Exception rescue ``` *E:* -``` + +```ruby Exception rescue AnError rescue ``` @@ -1088,7 +1116,7 @@ AnError rescue **Q33. 以下のコードがあります。** -``` +```ruby begin "hello".world rescue => ex @@ -1112,7 +1140,7 @@ end **Q34. 以下のコードがあります。** -``` +```ruby CustomError = Class.new(StandardError) def boom @@ -1144,7 +1172,7 @@ end **Q35. 以下のコードがあります。** -``` +```ruby def greeting "hello" ensure @@ -1160,27 +1188,27 @@ puts greeting *A:* -``` +```ruby hello ``` *B:* -``` +```ruby hi ``` *C:* -``` +```ruby Ensure called! hello ``` *D:* -``` +```ruby Ensure called! hi ``` @@ -1189,7 +1217,7 @@ hi **Q36. 以下のコードがあります。** -``` +```ruby class Identity def self.this_object self @@ -1214,25 +1242,28 @@ p c == d *A:* -``` +```ruby true true ``` *B:* -``` + +```ruby true false ``` *C:* -``` + +```ruby false true ``` *D:* -``` + +```ruby false false ``` @@ -1241,7 +1272,7 @@ false **Q37. 以下のコードがあります。** -``` +```ruby class Identity def self.this_object self.class @@ -1259,25 +1290,29 @@ p Identity.new.this_object.class **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby Identity Identity ``` *B:* -``` + +```ruby Class Identity ``` *C:* -``` + +```ruby Object Identity ``` *D:* -``` + +```ruby Class Object ``` @@ -1286,7 +1321,7 @@ Object **Q38. 以下のコードがあります。** -``` +```ruby module Mixin def this_object self @@ -1327,7 +1362,7 @@ p Identity.new.this_object.class **Q40. 以下のコードがあります。** -``` +```ruby module Mixin def self.greet puts "Hello World!" @@ -1353,7 +1388,7 @@ end **Q41. 以下の実行結果を出力するコードがあります。** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1384,7 +1419,7 @@ Hello World! **Q42. 以下の実行結果を出力するコードがあります。** -``` +```ruby module Mixin def greet puts "Hello World!" @@ -1415,7 +1450,7 @@ Hello World! **Q43. 以下の実行結果を出力するコードがあります。** -``` +```ruby class BaseClass private @@ -1439,19 +1474,19 @@ Hello World! *A:* -``` +```ruby public :greet ``` *B:* -``` +```ruby protected :greet ``` *C:* -``` +```ruby def greet super end @@ -1459,7 +1494,7 @@ end *D:* -``` +```ruby alias_method :original_greet, :greet def greet @@ -1471,7 +1506,7 @@ end **Q44. 以下の実行結果を出力するコードがあります。** -``` +```ruby h = [1, 2, 3] case h __(1)__ [x, y] @@ -1498,7 +1533,7 @@ end **Q45. 以下の実行結果を出力するコードがあります。** -``` +```ruby class Alphabet include Enumerable @@ -1539,7 +1574,7 @@ p set.select { |e| e > "W" } **Q46. 以下のコードがあります。** -``` +```ruby class TShirt SIZES = [:xs, :s, :m, :l, :xl, :xxl] @@ -1569,7 +1604,7 @@ p medium >= large *A:* -``` +```ruby true false true @@ -1578,7 +1613,8 @@ true ``` *B:* -``` + +```ruby false true true @@ -1587,7 +1623,8 @@ false ``` *C:* -``` + +```ruby false false false @@ -1596,7 +1633,8 @@ true ``` *D:* -``` + +```ruby false false false @@ -1611,7 +1649,7 @@ false **Q47. 以下のコードがあります。** -``` +```ruby require "date" date = Date.new(2000, 2, 24) @@ -1623,25 +1661,29 @@ puts(date >> 12) **実行結果として正しいものを選択してください。(1つ選択)** *A:* -``` + +```ruby 2000-02-12 2000-03-07 ``` *B:* -``` + +```ruby 2000-03-07 2000-02-12 ``` *C:* -``` + +```ruby 1999-02-24 2001-02-24 ``` *D:* -``` + +```ruby 1988-02-24 2012-02-24 ``` @@ -1650,7 +1692,7 @@ puts(date >> 12) **Q48. 以下の実行結果を出力するコードがあります。** -``` +```ruby require "time" t = Time.__(1)__("00000024021993", "%S%M%H%d%m%Y") @@ -1674,7 +1716,7 @@ puts t.iso8601 **Q49. 以下の実行結果を出力するコードがあります。** -``` +```ruby require "singleton" class Foo @@ -1703,7 +1745,7 @@ true **Q50. 以下の実行結果を出力するコードがあります。** -``` +```ruby require 'forwardable' class List diff --git a/silver.md b/silver.md index 975c407..538f744 100644 --- a/silver.md +++ b/silver.md @@ -32,7 +32,7 @@ **Q4. Which of the following can be inserted into `__(1)__` in order to generate the output below? (Choose two.)** -``` +```ruby $code = "CODE" __(1)__ @@ -49,7 +49,7 @@ i like writing CODE **Q5. Given the following:** -``` +```ruby num = 025 puts num ``` @@ -65,7 +65,7 @@ puts num **Q6. Given the following:** -``` +```ruby x = "Hello" y = x.empty? ? 1 : 2 p y @@ -82,7 +82,7 @@ p y **Q7. Given the following:** -``` +```ruby amount = 120 size = case amount @@ -106,7 +106,7 @@ p size **Q8. Given the following:** -``` +```ruby item = "apple" ["banana", "carrot", "daikon"].each do |item| @@ -125,7 +125,8 @@ A syntax error occurs An exception is raised *(c)* -``` + +```ruby banana carrot daikon @@ -133,7 +134,8 @@ daikon ``` *(d)* -``` + +```ruby banana carrot daikon @@ -144,7 +146,7 @@ apple **Q9. Given the following:** -``` +```ruby x = 0 4.times do |i| @@ -166,7 +168,7 @@ p x **Q10. Given the following:** -``` +```ruby s = "abcde" p s.each_char.map { |i| i * 2 @@ -185,7 +187,7 @@ p s.each_char.map { |i| **Q11. Given the following:** -``` +```ruby p "cocoa".chars.tally ``` @@ -200,7 +202,7 @@ p "cocoa".chars.tally **Q12. Which of the following can be inserted into `__(1)__` in order to generate the output below? (Choose one.)** -``` +```ruby puts "blah blah blah".__(1)__(/blah/, "yay") [Output] @@ -216,7 +218,7 @@ yay yay yay **Q13. Given the following:** -``` +```ruby s = "pear" if s.empty? @@ -239,7 +241,7 @@ end **Q14: Given the following:** -``` +```ruby ["foo: abc", "bar: 100"].each do |i| p i.slice(/[0-9]+/)&.to_i end @@ -248,19 +250,22 @@ end **Which is the correct result? (Choose one.)** *(a)* -``` + +```ruby 0 100 ``` *(b)* -``` + +```ruby nil 100 ``` *(c)* -``` + +```ruby false 100 ``` @@ -275,7 +280,7 @@ An error occurs at run-time. **Q15: Given the following:** -``` +```ruby def foo(x: 1, y: 2, z: 3) p [x, y, z] end @@ -294,7 +299,7 @@ foo(y: 4, z: 5) **Q16: Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby def foo(x:, y:, z:) p [x, y, z] end @@ -323,7 +328,7 @@ __(1)__ **Q18.Given the following:** -``` +```ruby MSG = 42 MSG += 5 p MSG @@ -340,7 +345,7 @@ p MSG **Q19. Given the following:** -``` +```ruby MSG = "hello" MSG.upcase! p MSG @@ -367,7 +372,7 @@ p MSG **Q21. Given the following:** -``` +```ruby x = [1,2,3,4,5,6,7,8] y = x x.reject! { |e| e.even? } @@ -379,25 +384,29 @@ p y *(a)* -``` + +```ruby [1, 3, 5, 7] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(b)* -``` + +```ruby [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(c)* -``` + +```ruby [1, 3, 5, 7] [1, 3, 5, 7] ``` *(d)* -``` + +```ruby [1, 3, 5, 7] [2, 4, 6, 8] ``` @@ -406,7 +415,7 @@ p y **Q22. Given the following:** -``` +```ruby a = [ 2, 4, 6, 8, 10 ] a.shift a.pop @@ -425,7 +434,7 @@ p a **Q23. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby x = true x __(1)__ exit(1) puts("succeeded!") @@ -444,7 +453,7 @@ succeeded! **Q24. Given the following:** -``` +```ruby m = true m or n = true p n @@ -462,7 +471,7 @@ p n **Q25. Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)** -``` +```ruby x = [ 9, 7, 5, 3, 1 ] p __(1)__ @@ -479,7 +488,7 @@ p __(1)__ **Q26. Which of the following can be inserted into __(1)__ in order for the given code to generate the output below? (Choose two.)** -``` +```ruby ary = [ 1, 2, 3, 4, 5 ] p ary.__(1)__ { |i| i.odd? } @@ -497,7 +506,7 @@ p ary.__(1)__ { |i| i.odd? } **Q27. Given the following:** -``` +```ruby puts "42A7".to_i ``` @@ -512,7 +521,7 @@ puts "42A7".to_i **Q28. Which of the following methods will NOT show you if the element `:c` exists in the hash key or not? (Choose one.)** -``` +```ruby h = {a: 2, b: 4, c: 6, d: 8, e: 10} ``` @@ -526,7 +535,7 @@ h = {a: 2, b: 4, c: 6, d: 8, e: 10} **Q29. "Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose two.)** -``` +```ruby a = [120, 40, 20, 80, 160, 60, 180] a.__(1)__ p a @@ -546,7 +555,7 @@ p a **Q30. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby p ["apple", "banana"] __(1)__ ["banana", "carrot"] [Output] @@ -563,7 +572,7 @@ p ["apple", "banana"] __(1)__ ["banana", "carrot"] **Q31. Given the following:** -``` +```ruby p %i(x1 x2 x3) ``` @@ -578,7 +587,7 @@ What is the correct result? (Choose one.) **Q32. Given the following:** -``` +```ruby class SomeError < StandardError; end class SomeOtherError < SomeError; end @@ -606,7 +615,7 @@ What is the correct result? (Choose one.) **Q33. Given the following:** -``` +```ruby begin ans = 100/0 puts ans @@ -660,7 +669,7 @@ Error: ZeroDivisionError **Q35. Given the following:** -``` +```ruby class Object def moo puts "MOO!" @@ -681,7 +690,7 @@ end **Q36. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby class Shouter def __(1)__(message) @message = message @@ -708,7 +717,7 @@ HELLO, WORLD! **Q37. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby class Shouter def initialize(message) @message = message @@ -735,7 +744,7 @@ HELLO, WORLD! **Q38. Given the following:** -``` +```ruby class Foo attr_reader :var def initialize @@ -766,7 +775,7 @@ puts bar.var **Q39. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose two.)** -``` +```ruby puts "$foo$".__(1)__("$") [Output] @@ -782,7 +791,7 @@ foo$ **Q40. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose one.)** -``` +```ruby r = "a".."e" p r.__(1)__ @@ -799,7 +808,7 @@ p r.__(1)__ **Q41. Given the following:** -``` +```ruby p [0,1,2,3,4,5].find {|x| x < 3} ``` @@ -814,7 +823,7 @@ p [0,1,2,3,4,5].find {|x| x < 3} **Q42. Which of the following can be inserted into `__(1)__` in order for the given code to generate the output below? (Choose two.)** -``` +```ruby p [1,16,8,4,2].__(1)__ [Output] @@ -831,7 +840,7 @@ p [1,16,8,4,2].__(1)__ **Q43. Which of the following can be inserted into __(1)__ in order for the given code to sort an array in descending order? (Choose one.)** -``` +```ruby ary = [2,4,8,1,16] p ary.__(1)__ @@ -848,7 +857,7 @@ p ary.__(1)__ **Q44. Given the following:** -``` +```ruby File.write("test", "hellorubyworld\n") File.open("test") do |file| file.seek(5) @@ -868,7 +877,7 @@ end **Q45. The code below was used to open a file omitting the second argument of the open method. In this case, which of the following is implicitly specified? (Choose one.)** -``` +```ruby file = open("sample.txt") ``` @@ -885,7 +894,7 @@ file = open("sample.txt") **In the case that the “test_two.txt” file already exist, this code should set first the file size to zero and then overwrites its content from the beginning. (Choose two.)** -``` +```ruby open("test_one.txt") {|source| open("test_two.txt", "__(1)__") {|dest| dest.write(source.read) @@ -913,7 +922,7 @@ open("test_one.txt") {|source| **Q48. Given the following:** -``` +```ruby p "hello ruby world"[6,4] ``` @@ -930,7 +939,7 @@ p "hello ruby world"[6,4] Given the following: -``` +```ruby str = "bat" str[1,1] = "o" p str @@ -947,7 +956,7 @@ Which is the correct output? (Choose one.) **Q50. Given the following:** -``` +```ruby puts 5 * "hi" ``` diff --git a/silver_answers_ja.md b/silver_answers_ja.md index 500378e..06cd549 100644 --- a/silver_answers_ja.md +++ b/silver_answers_ja.md @@ -247,7 +247,7 @@ Another approach is to use a range, which generates a subarray based the index v --------------------------------------------------------------------------- -**A28. (b)** +**A28: (b)** `has_key?`、`include?`、`key?`、`member?`はすべて同じメソッドの別名で、与えられたキーがハッシュに存在する場合`true`を、存在しない場合`false`を返します。 diff --git a/silver_ja.md b/silver_ja.md index 69c4ad7..be8fce1 100644 --- a/silver_ja.md +++ b/silver_ja.md @@ -66,7 +66,7 @@ puts num **Q6. 以下のコードがあります。** -``` +```ruby x = "Hello" y = x.empty? ? 1 : 2 p y @@ -83,7 +83,7 @@ p y **Q7. 以下のコードがあります。** -``` +```ruby amount = 120 size = case amount @@ -107,7 +107,7 @@ p size **Q8. 以下のコードがあります。** -``` +```ruby item = "apple" ["banana", "carrot", "daikon"].each do |item| @@ -126,7 +126,8 @@ puts item 例外が発生する *(c)* -``` + +```ruby banana carrot daikon @@ -134,7 +135,8 @@ daikon ``` *(d)* -``` + +```ruby banana carrot daikon @@ -145,7 +147,7 @@ apple **Q9. 以下のコードがあります。** -``` +```ruby x = 0 4.times do |i| @@ -167,7 +169,7 @@ p x **Q10. 以下のコードがあります。** -``` +```ruby s = "abcde" p s.each_char.map { |i| i * 2 @@ -186,7 +188,7 @@ p s.each_char.map { |i| **Q11. 以下のコードがあります。** -``` +```ruby p "cocoa".chars.tally ``` @@ -218,7 +220,7 @@ yay yay yay **Q13. 以下のコードがあります。** -``` +```ruby s = "pear" if s.empty? @@ -241,7 +243,7 @@ end **Q14: 以下のコードがあります。** -``` +```ruby ["foo: abc", "bar: 100"].each do |i| p i.slice(/[0-9]+/)&.to_i end @@ -250,19 +252,22 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *(a)* -``` + +```ruby 0 100 ``` *(b)* -``` + +```ruby nil 100 ``` *(c)* -``` + +```ruby false 100 ``` @@ -277,7 +282,7 @@ false **Q15: 以下のコードがあります。** -``` +```ruby def foo(x: 1, y: 2, z: 3) p [x, y, z] end @@ -326,7 +331,7 @@ __(1)__ **Q18.以下のコードがあります。** -``` +```ruby MSG = 42 MSG += 5 p MSG @@ -343,7 +348,7 @@ p MSG **Q19. 以下のコードがあります。** -``` +```ruby MSG = "hello" MSG.upcase! p MSG @@ -370,7 +375,7 @@ p MSG **Q21. 以下のコードがあります。** -``` +```ruby x = [1,2,3,4,5,6,7,8] y = x x.reject! { |e| e.even? } @@ -382,25 +387,29 @@ p y *(a)* -``` + +```ruby [1, 3, 5, 7] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(b)* -``` + +```ruby [1, 2, 3, 4, 5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8] ``` *(c)* -``` + +```ruby [1, 3, 5, 7] [1, 3, 5, 7] ``` *(d)* -``` + +```ruby [1, 3, 5, 7] [2, 4, 6, 8] ``` @@ -409,7 +418,7 @@ p y **Q22. 以下のコードがあります。** -``` +```ruby a = [ 2, 4, 6, 8, 10 ] a.shift a.pop @@ -448,7 +457,7 @@ succeeded! **Q24. 以下のコードがあります。** -``` +```ruby m = true m or n = true p n @@ -503,7 +512,7 @@ p ary.__(1)__ { |i| i.odd? } **Q27. 以下のコードがあります。** -``` +```ruby puts "42A7".to_i ``` @@ -518,7 +527,7 @@ puts "42A7".to_i **Q28. 次のメソッドのうち`:c`がハッシュのキーとして存在するかどうかを「返さない」ものを選択してください。(1つ選択)** -``` +```ruby h = {a: 2, b: 4, c: 6, d: 8, e: 10} ``` @@ -571,7 +580,7 @@ p ["apple", "banana"] __(1)__ ["banana", "carrot"] **Q31. 以下のコードがあります。** -``` +```ruby p %i(x1 x2 x3) ``` @@ -586,7 +595,7 @@ p %i(x1 x2 x3) **Q32. 以下のコードがあります。** -``` +```ruby class SomeError < StandardError; end class SomeOtherError < SomeError; end @@ -614,7 +623,7 @@ end **Q33. 以下のコードがあります。** -``` +```ruby begin ans = 100/0 puts ans @@ -629,7 +638,8 @@ end **実行結果として正しいものを選択してください。(1つ選択)** *(a)* -``` + +```ruby 0 DONE! ``` @@ -641,13 +651,15 @@ Error: ZeroDivisionError ``` *(c)* -``` + +```ruby Error: ZeroDivisionError DONE! ``` *(d)* -``` + +```ruby Error: ZeroDivisionError 0 ``` @@ -668,7 +680,7 @@ Error: ZeroDivisionError **Q35. 以下のコードがあります。** -``` +```ruby class Object def moo puts "MOO!" @@ -745,7 +757,7 @@ HELLO, WORLD! **Q38. 以下のコードがあります。** -``` +```ruby class Foo attr_reader :var def initialize @@ -811,7 +823,7 @@ p r.__(1)__ **Q41. 以下のコードがあります。** -``` +```ruby p [0,1,2,3,4,5].find {|x| x < 3} ``` @@ -862,7 +874,7 @@ p ary.__(1)__ **Q44. 以下のコードがあります。** -``` +```ruby File.write("test", "hellorubyworld\n") File.open("test") do |file| file.seek(5) @@ -882,7 +894,7 @@ end **Q45. 以下のコードではopenメソッドの第2引数を省略してファイルを開いています。このケースで暗黙的に第2引数として指定されるものを選択してください。** -``` +```ruby file = open("sample.txt") ``` @@ -928,7 +940,7 @@ open("test_one.txt") {|source| **Q48. 以下のコードがあります。** -``` +```ruby p "hello ruby world"[6,4] ``` @@ -962,7 +974,7 @@ p str **Q50. 以下のコードがあります。** -``` +```ruby puts 5 * "hi" ``` diff --git a/test.rb b/test.rb new file mode 100644 index 0000000..65a7658 --- /dev/null +++ b/test.rb @@ -0,0 +1,188 @@ +require "tty-markdown" +require "timeout" + +class Test + attr_reader :questions, :answers, :howto + + DURATION = 60 * 90 + + class Exit < StandardError; end + + class EndOfTime < StandardError; end + + def initialize(type: :silver, language: :en) + case type + when :silver + @questions = File.read("silver#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] + @answers = File.read("silver_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] + @answers.map! do |ans| + ans.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i| + i.ord - "a".ord + } + end + @test_symbol = "a" + when :gold + @questions = File.read("gold#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] + @answers = File.read("gold_answers#{(language == :en) ? "" : "_ja"}.md").split(/^-------------.*\n/)[0..49] + @answers.map! do |ans| + ans.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i| + i.ord - "A".ord + } + end + @test_symbol = "A" + else + raise StandardError, "test not found" + end + @howto = File.read("test_readme.md") + @last_printed_lines_count = 0 + end + + def start + clean_the_screen + print_howto + clean_the_screen + Timeout.timeout(DURATION, EndOfTime) do + @exam_started = true + exam + end + rescue Exit + clean_the_screen + calc_and_print_result(@result) + rescue EndOfTime + clean_the_screen + puts "# Time for exam has runned out" + calc_and_print_result(@result) + end + + private + + def exam + @curr_inx = -1 + @result = Array.new(50) + @user_answers = Array.new(50) + loop do + next_question + user_input = get_user_input + case user_input + when :repeat + @curr_inx -= 1 + next + when :next + next + when :prev + @curr_inx -= 2 + next + when :howto + @curr_inx -= 1 + clean_the_screen + print_howto + clean_the_screen + else + @user_answers[@curr_inx] = user_input + @result[@curr_inx] = validate_answer(user_input) + end + end + end + + def next_question + @curr_inx += 1 if @curr_inx < 49 + clean_the_screen + question = questions[@curr_inx] + puts question + if @user_answers[@curr_inx] + prev_answ = @user_answers[@curr_inx].map { |i| (@test_symbol.ord + i).chr }.join(",") + puts "## Your previously entered answer is **(#{prev_answ})**" + end + end + + def validate_answer(user_answers) + user_answers.sort == answers[@curr_inx].sort + end + + def get_user_input + puts "**Write you answers separated by commas:**" + user_answer = gets.chomp + case user_answer + in /exit/i + puts "**Are you sure want finish exam? (Enter `yes` to exit)**" + raise Exit if gets.match?(/yes/i) + + :repeat + in /\A(n|next|continue)\z/i + :next + in /\A(p|previous|prev\z)/i + :prev + in /\Agoto (\d+)\z/ + return :repeat unless $1.to_i.between?(1, 50) + + @curr_inx = $1.to_i - 1 + :repeat + in /\A(stop|finish|end\z)/i + puts "**Are you sure want finish exam? (Enter `yes` to exit)**" + raise Exit if gets.match?(/yes/i) + + :repeat + in /\A(help|h)\z/i + puts <<~MD + # Available commands + - `howto` - print the text that was printend at start (don't restart the exam) + - `n` or `next` or `continue` - to go to the next question in order + - `p` or `prev` or `previous` - to go to the previous question in order + - `stop` or `finish` or `end` - to finish the exam and print your results + - `h` or `help` - to print this text + + **Press enter to continue** + MD + gets + + :repeat + in /\Ahowto\z/i + :howto + else + user_answer.strip.split(",").map { |e| e.ord - @test_symbol.ord } + end + end + + def calc_and_print_result(result) + correct_answers_count = result.count { |e| 1 if e } + correct_answers_percecnt = correct_answers_count.to_f / questions.length * 100 + puts "Your result is **#{correct_answers_count} out of #{questions.length}**" + puts "Percent of **correct answers is #{correct_answers_percecnt.round(2)}%**" + if correct_answers_count >= 75.0 + puts "## You've passed the test exam" + else + puts "## You've failed the test exam." + puts "### Don't give up! You can do it! (=" + end + end + + def print_howto + puts howto.to_s + if @exam_started + puts "**Press enter to continue**" + gets + else + puts "## To start the test enter `y`" + raise Exit unless gets.chomp.match?(/\Ay\z/) + end + end + + def clean_the_screen + print "\033[2J\033[3J\033[1;1H" + end + + def puts(string) + Kernel.puts TTY::Markdown.parse(string).to_s + end +end + +test_type = ARGV[0]&.to_sym || :silver +test_language = ARGV[1]&.to_sym || :en + +ARGV.clear +if test_type.match?("help") + puts TTY::Markdown.parse(File.read("test_help.md")).to_s +else + test = Test.new(type: test_type, language: test_language) + test.start +end diff --git a/test_help.md b/test_help.md new file mode 100644 index 0000000..e5a05c3 --- /dev/null +++ b/test_help.md @@ -0,0 +1,15 @@ +Usage: `ruby test.rb [testname] [locale]` + +**testname**: + pass the name of the test that you want to run + could be a `gold` or `silver` or `help` + the default value is `silver` + `help` would print this message + Example: + `ruby test.rb gold` +**locale**: + can be `en` or `ja` or `jp` + default is `en`. + [secret information] + if you pass any word that isn't `en` + it would accepted as `jp` =) diff --git a/test_readme.md b/test_readme.md new file mode 100644 index 0000000..d8064cb --- /dev/null +++ b/test_readme.md @@ -0,0 +1,37 @@ +# Welcome to ruby exam preparation + +## ATTENTION +If you keep the test it would flush your terminal buffer. +Enter `y` in case you agree with that. + +### Notice +Recomend **open this test in new terminal** +for simply navigation over printed questions + +### Duration +The test duration is **90 minutes**. +If you exceed the time, test would be interrupted +and you will get the result + +### How to answer +When you'll answer on questions please + use the same register of bullet points as used in questions. + +#### Example: + +**Q1 Some text of question** + +- (A) +```ruby + puts "answer" + # here might be the code or smth +``` +- (B) answer +- (C) answer +- (D) answer + +Write you answers separated by commas: + +A,B,C + +----------------------------------------