summaryrefslogtreecommitdiff
path: root/lib/pry/warning.rb
blob: b37478f46dc52aafe9e9471e452f37dc6a6c52be (plain)
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
# frozen_string_literal: true

class Pry
  # @api private
  # @since ?.?.?
  module Warning
    # Prints a warning message with exact file and line location, similar to how
    # Ruby's -W prints warnings.
    #
    # @param [String] message
    # @return [void]
    def self.warn(message)
      if Kernel.respond_to?(:caller_locations)
        location = caller_locations(2..2).first
        path = location.path
        lineno = location.lineno
      else
        # Ruby 1.9.3 support.
        frame = caller[1].split(':') # rubocop:disable Performance/Caller
        path = frame.first
        lineno = frame[1]
      end

      Kernel.warn("#{path}:#{lineno}: warning: #{message}")
    end
  end
end