1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/yes_i_did.rb', line 1
def yes
error = proc do |msg|
puts IRB::Color.colorize("yes: #{msg}", ['RED'])
return
end
exception = $!
error.("last command didn't raise an exception") if exception.nil?
error.("unsupported error type #{exception.class}") unless exception.is_a?(NameError)
corrections = exception.corrections
error.("there are multiple possible corrections") if corrections.length > 1
error.("there are no corrections") if corrections.empty?
checker = DidYouMean::NameErrorCheckers.new(exception)
case checker
when DidYouMean::ClassNameChecker
original_name = checker.class_name
when DidYouMean::VariableNameChecker
original_name = checker.name
else
error.("couldn't find a matching DidYouMean checker - this is a bug!")
end
original_name = original_name.to_s
previous_command = IRB.CurrentContext.io.line(-2)
replacements_made = 0
fixed_command = previous_command.gsub(/[a-zA-Z_][a-zA-Z_0-9]*[\?!]?/) do |match|
if match == original_name
replacements_made += 1
corrections.first.to_s
else
match
end
end
error.('couldn\'t make replacements, sorry!') if replacements_made.zero?
prompt_pad = ' ' * IRB.CurrentContext.io.prompt.length
output_fixed_command = fixed_command.each_line.map { |line| prompt_pad + line }.join.chomp
message = IRB::Color.colorize(output_fixed_command, ['GREEN'])
puts message
eval(fixed_command, IRB.CurrentContext.workspace.binding)
end
|