Class: ProblemCodeAI
- Inherits:
-
BaseCodeAI
- Object
- BaseCodeAI
- ProblemCodeAI
- Defined in:
- lib/asker/ai/code/problem_code_ai.rb
Overview
Class for RubyCodeAI objects
Instance Attribute Summary
Attributes inherited from BaseCodeAI
Instance Method Summary collapse
-
#initialize(code) ⇒ ProblemCodeAI
constructor
A new instance of ProblemCodeAI.
-
#make_comment_error ⇒ Object
Make errors about comments.
-
#make_no_error_changes ⇒ Object
Make questions without errors.
-
#make_syntax_error ⇒ Object
Make questions with syntax errors.
-
#make_variable_error ⇒ Object
Make questions with variable errors.
Methods inherited from BaseCodeAI
#clone_array, #filename, #lines, #lines_to_html, #lines_to_s, #make_questions, #name, #num, #process?, #type
Constructor Details
#initialize(code) ⇒ ProblemCodeAI
Returns a new instance of ProblemCodeAI.
9 10 11 12 13 14 |
# File 'lib/asker/ai/code/problem_code_ai.rb', line 9 def initialize(code) @reduce = 1 @reduce = 4 if code.lines.size > 25 @lang = LangFactory.instance.get('es') super code end |
Instance Method Details
#make_comment_error ⇒ Object
Make errors about comments
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/asker/ai/code/problem_code_ai.rb', line 18 def make_comment_error questions = [] error_lines = [] @lines.each_with_index do |line,index| if line.strip.start_with?('#') lines = clone_array @lines lines[index].sub!('#','').strip! q = Question.new(:short) q.name = "#{name}-#{num}-uncomment" q.text = @lang.text_for(:code1,lines_to_html(lines)) q.shorts << (index+1) q.feedback = 'Comment symbol removed' questions << q elsif line.strip.size>0 lines = clone_array @lines lines[index]='# ' + lines[index] q = Question.new(:short) q.name = "#{name}-#{num}-comment" q.text = @lang.text_for(:code1,lines_to_html(lines)) q.shorts << (index+1) q.feedback = 'Comment symbol added' questions << q end end questions.shuffle[0,@lines.size/@reduce] end |
#make_no_error_changes ⇒ Object
Make questions without errors
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/asker/ai/code/problem_code_ai.rb', line 49 def make_no_error_changes questions = [] empty_lines = [] used_lines = [] @lines.each_with_index do |line,index| if line.strip.size.zero? empty_lines << index else used_lines << index end end used_lines.each do |index| lines = clone_array(@lines) lines.insert(index, ' ' * (rand(4).to_i + 1)) if @lines.size < 4 || rand(2) == 0 q = Question.new(:short) q.name = "#{name}-#{num}-codeok" q.text = @lang.text_for(:code1,lines_to_html(lines)) q.shorts << '0' q.feedback = 'Code is OK' questions << q else q = Question.new(:choice) q.name = "#{name}-#{num}-codeok" q.text = @lang.text_for(:code2,lines_to_html(lines)) others = (1..@lines.size).to_a.shuffle! q.good = '0' q.bads << others[0].to_s q.bads << others[1].to_s q.bads << others[2].to_s q.feedback = 'Code is OK' end end questions.shuffle[0,@lines.size/@reduce] end |
#make_syntax_error ⇒ Object
Make questions with syntax errors
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/asker/ai/code/problem_code_ai.rb', line 89 def make_syntax_error questions = [] @lang.mistakes.each_pair do |key,values| error_lines = [] @lines.each_with_index do |line,index| error_lines << index if line.include?(key.to_s) end v = values.split(',') v.each do |value| error_lines.each do |index| lines = clone_array(@lines) lines[index].sub!(key.to_s, value) if @lines.size < 4 || rand(2) == 0 q = Question.new(:short) q.name = "#{name}-#{num}-syntaxerror" q.text = @lang.text_for(:code1,lines_to_html(lines)) q.shorts << (index+1) q.feedback = "Syntax error: '#{value}' must be '#{key}'" else q = Question.new(:choice) q.name = "#{name}-#{num}-syntaxerror" q.text = @lang.text_for(:code2,lines_to_html(lines)) others = (1..@lines.size).to_a.shuffle! others.delete(index+1) q.good = (index + 1).to_s q.bads << others[0].to_s q.bads << others[1].to_s q.bads << others[2].to_s q.feedback = "Syntax error: '#{value}' must be '#{key}'" end questions << q end end end questions.shuffle[0,@lines.size/@reduce] end |
#make_variable_error ⇒ Object
Make questions with variable errors
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/asker/ai/code/problem_code_ai.rb', line 130 def make_variable_error questions = [] error_lines = [] @lines.each_with_index do |line, index| # Search Variable assignment m = /\s*(\w*)\s*\=\w*/.match(line) i = [] unless m.nil? varname = (m.values_at 1)[0] # Search used Variable @lines.each_with_index do |line2, index2| next if index >= index2 i << index2 if line2.include?(varname) end end next if i.size == 0 i.shuffle! i.each do |k| lines = clone_array @lines temp = lines[index] lines[index] = lines[k] lines[k] = temp if rand(2) == 0 q = Question.new(:short) q.name = "#{name}-#{num}-variable" q.text = @lang.text_for(:code1, lines_to_html(lines)) q.shorts << (index + 1) q.feedback = "Variable error! Swapped lines #{(index+1)} with #{(k+1)}" else q = Question.new(:choice) q.name = "#{name}-#{num}-variable" q.text = @lang.text_for(:code2, lines_to_html(lines)) others = (1..@lines.size).to_a.shuffle! others.delete(index+1) q.good = (index + 1).to_s q.bads << others[0].to_s q.bads << others[1].to_s q.bads << others[2].to_s q.feedback = "Variable error! Swapped lines #{(index+1)} with #{(k+1)}" end questions << q end end questions.shuffle[0,@lines.size/@reduce] end |