Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#yesObject



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
  # This is a proc so that it can return from the "yes" function
  error = proc do |msg|
    puts IRB::Color.colorize("yes: #{msg}", ['RED'])
    return
  end

  # Get the invalid name and the suggestion replacements
  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?

  # Get the original, incorrect name as a string
  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

  # Get the last IRB command run
  previous_command = IRB.CurrentContext.io.line(-2)

  # Replace the original with the correction, but only replace a complete
  # identifier
  replacements_made = 0
  fixed_command = previous_command.gsub(/[a-zA-Z_][a-zA-Z_0-9]*[\?!]?/) do |match|
    if match == original_name
      # Replace with correction
      replacements_made += 1
      corrections.first.to_s
    else
      # Keep the same
      match
    end
  end
  error.('couldn\'t make replacements, sorry!') if replacements_made.zero?

  # Print the altered code
  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
  
  # Run it
  eval(fixed_command, IRB.CurrentContext.workspace.binding)
end