Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.0
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ruby Association Certified Ruby Programmer Examination

Sample questions for [Ruby Association Certified Ruby Programmer Examination](https://www.ruby.or.jp/en/certification/examination/)
Sample questions for [Ruby Association Certified Ruby Programmer Examination version 3](https://www.ruby.or.jp/en/certification/examination/)

* [Sample Questions for Silver](silver.md)
* [Sample Questions for Gold](gold.md)
* [Sample Questions for Silver](silver.md) ([Japanese](silver_ja.md))
* [Sample Questions for Gold](gold.md) ([Japanese](gold_ja.md))
80 changes: 80 additions & 0 deletions check_gold.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require "open3"
require "rbconfig"

$questions = File.read("gold.md").split(/^-------------.*\n/)
$answers = File.read("gold_answers.md").split(/^-------------.*\n/)

$ruby = File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])

def check(n)
question = $questions[n - 1]
answer = $answers[n - 1]
case question
when /Assume that the following code must have the stated execution result|Which code produces the following execution result\?/
s = question.slice(/^```\n(.*?)^```\n/m, 1)
code, output = s.split(/\n?\[Execution Result\]\n+/)
options = question.scan(/^\*[A-Z]:\* `(.*)`/).flatten(1)
if options.empty?
options = question.scan(/^\*[A-Z]:\*.*\n((?:(?!\*[A-Z]:\*).*\n)*)/).flatten(1).map { |i|
i.slice(/^```\n(.*)^```\n/m, 1) || i
}
end
xs = answer.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i|
i.ord - ?A.ord
}
if question.match?(/Which of the following options CANNOT be inserted into/)
xs = (0 .. options.size - 1).to_a - xs
end
ys = options.each_with_index.filter_map { |option, i|
c = code.strip.empty? ? option : c = code.gsub(/__\(1\)__/, option)
o, = Open3.capture2e($ruby, stdin_data: c)
if $VERBOSE
p [i, o, output]
end
o == output ? i : nil
}
result = xs == ys ? "OK" : "NG"
puts "Q#{n}: #{result}"
when /Which option corresponds to the execution result\?/
code = question.slice(/^```\n(.*?)^```\n/m, 1)
options = question.scan(/^(?:- )?\*[A-Z]:\* (.+)/).flatten(1).map { |i|
i.slice(/`(.*)`/, 1) || i
}
if options.empty?
options = question.scan(/^\*[A-Z]:\*.*\n((?:(?!\*[A-Z]:\*).*\n)*)/).flatten(1).map { |i|
i.slice(/^```\n(.*)^```\n/m, 1) || i
}
end
output, = Open3.capture2e($ruby, stdin_data: code)
if $VERBOSE
p [output, options]
end
xs = answer.slice(/^\*\*A\d+:.*/).scan(/\(([A-Z])\)/).flatten(1).map { |i|
i.ord - ?A.ord
}
ys = options.each_with_index.filter_map { |option, i|
case option
when /no output/i
output.strip.empty?
when /a syntax error/i
output.match?(/syntax error/)
when /an exception is raised|an error occurs/i
output.match?(/\(.*Error\)/)
else
output.strip == option.strip
end ? i : nil
}
result = xs == ys ? "OK" : "NG"
puts "Q#{n}: #{result}"
else
puts "Q#{n}: ?"
end
end

if ARGV[0]
check(ARGV[0].to_i)
else
(1..50).each do |i|
check(i)
end
end
72 changes: 72 additions & 0 deletions check_silver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require "open3"
require "rbconfig"

$questions = File.read("silver.md").split(/^-------------.*\n/)
$answers = File.read("silver_answers.md").split(/^-------------.*\n/)

$ruby = File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"])

def check(n)
question = $questions[n - 1]
answer = $answers[n - 1]
case question
when /Which of the following can be inserted into/
s = question.slice(/^```\n(.*?)^```\n/m, 1)
code, output = s.split(/\n\[Output\]\n/)
options = question.scan(/^- \([a-z]\) `(.*)`/).flatten(1)
xs = answer.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i|
i.ord - ?a.ord
}
ys = options.each_with_index.filter_map { |option, i|
c = code.gsub(/__\(1\)__/, option)
o, = Open3.capture2e($ruby, stdin_data: c)
if $VERBOSE
p [i, o, output]
end
o == output ? i : nil
}
result = xs == ys ? "OK" : "NG"
puts "Q#{n}: #{result}"
when /Given the following:/
code = question.slice(/^```\n(.*?)^```\n/m, 1)
options = question.scan(/^- \([a-z]\) (.*)/).flatten(1).map { |i|
i.slice(/`(.*)`/, 1) || i
}
if options.empty?
options = question.scan(/^\*\([a-z]\).*\n((?:(?!\*\([a-z]\)).*\n)*)/).flatten(1).map { |i|
i.slice(/^```\n(.*)^```\n/m, 1) || i
}
end
output, = Open3.capture2e($ruby, stdin_data: code)
if $VERBOSE
p output
end
xs = answer.slice(/^\*\*A\d+:.*/).scan(/\(([a-z])\)/).flatten(1).map { |i|
i.ord - ?a.ord
}
ys = options.each_with_index.filter_map { |option, i|
case option
when /no output/i
output.strip.empty?
when /a syntax error/i
output.match?(/syntax error/)
when /an exception is raised|an error occurs/i
output.match?(/\(.*Error\)/)
else
output.strip == option.strip
end ? i : nil
}
result = xs == ys ? "OK" : "NG"
puts "Q#{n}: #{result}"
else
puts "Q#{n}: ?"
end
end

if ARGV[0]
check(ARGV[0].to_i)
else
(1..50).each do |i|
check(i)
end
end
Loading