Module: MaybeYouMeant

Defined in:
lib/maybeyoumeant.rb,
lib/maybeyoumeant.rb

Defined Under Namespace

Modules: Config, ObjectExtentions Classes: Levenshtein, NilLogger, StdErrLogger

Class Method Summary collapse

Class Method Details

.irb_session?Boolean

Returns true if in an IRB session, false if not.

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/maybeyoumeant.rb', line 26

def self.irb_session?
  begin
    @irb ||= Kernel.const_get('IRB') && Kernel.const_get('Readline') && Readline.const_defined?('HISTORY')
  rescue NameError
    @irb = false
  end
  return @irb
end

.log(msg = nil, &block) ⇒ Object

Logs a message to the current logger. The current logger is either STDOUT (be default), or a nil logger if debug is disabled.



21
22
23
# File 'lib/maybeyoumeant.rb', line 21

def self.log(msg = nil, &block)
  @logger.log(msg, &block)
end

.logger=(logger) ⇒ Object



14
15
16
# File 'lib/maybeyoumeant.rb', line 14

def self.logger=(logger)
  @logger = logger
end

.tweak_history(method, nearby) ⇒ Object

Updates IRB history to include the fixed command. For example if:

'hello'.ucase

is executed, IRB history will be update with

'hello'.upcase


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/maybeyoumeant.rb', line 40

def self.tweak_history(method, nearby)
  return unless irb_session? && !Readline::HISTORY.empty?

  line = Readline::HISTORY[Readline::HISTORY.size - 1]
  Readline::HISTORY.pop if Config::remove_from_history

  # Try to match .#{method}\W before replacing all occurrences of method.
  line.gsub!(/(\W|^)#{method.to_s}((?=\W)|$)/, "\\1#{nearby.to_s}")
  log((Paint["Maybe you meant: ", :red]) + line.to_s)

  Readline::HISTORY.push line if Config.add_to_history
end