yes_i_did 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/yes_i_did.rb +56 -0
  3. data/yes_i_did.gemspec +14 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38a00ca0efe986b3fcf6ef4045baaeca7a8258c87e1351f5157c8ffe8fc6b529
4
+ data.tar.gz: a69948f3cbbb3c9a013eb493940f4e1fddc2456f46d957fece1935358fbea8ea
5
+ SHA512:
6
+ metadata.gz: f6d4cad5cbdaffd0e6a8d9ad7972ecf765a9b42a1f5920e3dbc0f02814af6ffb80b1b227e801ddb890b9b8101fdeb634f222efee49326fd7f555ff1d9de38b27
7
+ data.tar.gz: 6931a78b93a0d98e5c480e5216af439a0b615ecacf2bdbad0033d21b941b0ccedd6cf2dbed27820ef77a8be3be6945849fff7ddfcdf6573e59417db8c57841bd
@@ -0,0 +1,56 @@
1
+ def yes
2
+ # This is a proc so that it can return from the "yes" function
3
+ error = proc do |msg|
4
+ puts IRB::Color.colorize("yes: #{msg}", ['RED'])
5
+ return
6
+ end
7
+
8
+ # Get the invalid name and the suggestion replacements
9
+ exception = $!
10
+
11
+ error.("last command didn't raise an exception") if exception.nil?
12
+ error.("unsupported error type #{exception.class}") unless exception.is_a?(NameError)
13
+
14
+ corrections = exception.corrections
15
+ error.("there are multiple possible corrections") if corrections.length > 1
16
+ error.("there are no corrections") if corrections.empty?
17
+
18
+ # Get the original, incorrect name as a string
19
+ checker = DidYouMean::NameErrorCheckers.new(exception)
20
+ case checker
21
+ when DidYouMean::ClassNameChecker
22
+ original_name = checker.class_name
23
+ when DidYouMean::VariableNameChecker
24
+ original_name = checker.name
25
+ else
26
+ error.("couldn't find a matching DidYouMean checker - this is a bug!")
27
+ end
28
+ original_name = original_name.to_s
29
+
30
+ # Get the last IRB command run
31
+ previous_command = IRB.CurrentContext.io.line(-2)
32
+
33
+ # Replace the original with the correction, but only replace a complete
34
+ # identifier
35
+ replacements_made = 0
36
+ fixed_command = previous_command.gsub(/[a-zA-Z_][a-zA-Z_0-9]*[\?!]?/) do |match|
37
+ if match == original_name
38
+ # Replace with correction
39
+ replacements_made += 1
40
+ corrections.first.to_s
41
+ else
42
+ # Keep the same
43
+ match
44
+ end
45
+ end
46
+ error.('couldn\'t make replacements, sorry!') if replacements_made.zero?
47
+
48
+ # Print the altered code
49
+ prompt_pad = ' ' * IRB.CurrentContext.io.prompt.length
50
+ output_fixed_command = fixed_command.each_line.map { |line| prompt_pad + line }.join.chomp
51
+ message = IRB::Color.colorize(output_fixed_command, ['GREEN'])
52
+ puts message
53
+
54
+ # Run it
55
+ eval(fixed_command, IRB.CurrentContext.workspace.binding)
56
+ end
@@ -0,0 +1,14 @@
1
+
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "yes_i_did"
4
+ spec.version = '1.0.0'
5
+ spec.authors = ["Aaron Christiansen"]
6
+ spec.email = ["hello@aaronc.cc"]
7
+
8
+ spec.summary = %q{Re-run code with did_you_mean suggestions in IRB}
9
+ spec.homepage = "https://github.com/AaronC81/yes_i_did"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = ['lib/yes_i_did.rb', 'yes_i_did.gemspec']
13
+ spec.require_paths = ["lib"]
14
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yes_i_did
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Christiansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - hello@aaronc.cc
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/yes_i_did.rb
21
+ - yes_i_did.gemspec
22
+ homepage: https://github.com/AaronC81/yes_i_did
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Re-run code with did_you_mean suggestions in IRB
45
+ test_files: []