Class: RubyCodeAI

Inherits:
BaseCodeAI show all
Defined in:
lib/asker/ai/code/ruby_code_ai.rb

Instance Attribute Summary

Attributes inherited from BaseCodeAI

#questions

Instance Method Summary collapse

Methods inherited from BaseCodeAI

#clone_array, #filename, #lines, #lines_to_html, #lines_to_s, #make_questions, #name, #num, #process?, #type

Constructor Details

#initialize(code) ⇒ RubyCodeAI

Returns a new instance of RubyCodeAI.



6
7
8
9
10
11
# File 'lib/asker/ai/code/ruby_code_ai.rb', line 6

def initialize(code)
  @reduce = 1
  @reduce = 4 if code.lines.size > 25
  @lang = LangFactory.instance.get("ruby")
  super code
end

Instance Method Details

#make_comment_errorObject

Make errors about comments



15
16
17
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
# File 'lib/asker/ai/code/ruby_code_ai.rb', line 15

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}-code1uncomment"
      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}-code1comment"
      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_changesObject

Make questions without errors



46
47
48
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
# File 'lib/asker/ai/code/ruby_code_ai.rb', line 46

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}-code1ok"
      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}-code1ok"
      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_errorObject

Make questions with syntax errors



86
87
88
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
# File 'lib/asker/ai/code/ruby_code_ai.rb', line 86

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}-code1syntaxerror"
          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}-code1syntaxerror"
          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_errorObject

Make questions with variable errors



127
128
129
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
# File 'lib/asker/ai/code/ruby_code_ai.rb', line 127

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}-code1variable"
        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}-code1variable"
        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