File: method.rb

package info (click to toggle)
ruby-byebug 12.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,304 kB
  • sloc: ruby: 9,013; ansic: 1,678; sh: 5; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,330 bytes parent folder | download | duplicates (4)
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
# frozen_string_literal: true

require_relative "../command"
require_relative "../helpers/eval"

module Byebug
  #
  # Show methods of specific classes/modules/objects.
  #
  class MethodCommand < Command
    include Helpers::EvalHelper

    self.allow_in_post_mortem = true

    def self.regexp
      /^\s* m(?:ethod)? \s+ (i(:?nstance)?\s+)?/x
    end

    def self.description
      <<-DESCRIPTION
        m[ethod] (i[nstance][ <obj>]|<class|module>)

        #{short_description}

        When invoked with "instance", shows instance methods of the object
        specified as argument or of self no object was specified.

        When invoked only with a class or module, shows class methods of the
        class or module specified as argument.
      DESCRIPTION
    end

    def self.short_description
      "Shows methods of an object, class or module"
    end

    def execute
      obj = warning_eval(@match.post_match)

      result =
        if @match[1]
          prc("method.methods", obj.methods.sort) { |item, _| { name: item } }
        elsif !obj.is_a?(Module)
          pr("variable.errors.not_module", object: @match.post_match)
        else
          prc("method.methods", obj.instance_methods(false).sort) do |item, _|
            { name: item }
          end
        end
      puts result
    end
  end
end