module MaybeYouMeant

Public Class Methods

irb_session?() click to toggle source

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

# 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) click to toggle source

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

# File lib/maybeyoumeant.rb, line 21
def self.log(msg = nil, &block)
  @logger.log(msg, &block)
end
logger=(logger) click to toggle source
# File lib/maybeyoumeant.rb, line 14
def self.logger=(logger)
  @logger = logger
end
tweak_history(method, nearby) click to toggle source

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

'hello'.ucase

is executed, IRB history will be update with

'hello'.upcase
# 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