Module: What

Defined in:
lib/what.rb

Defined Under Namespace

Modules: Colors, NoColors

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.about(object, method = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/what.rb', line 111

def What.about(object, method = nil)
  methods = []
  if Symbol === method
    methods = [object.method(method)].group_by { |m| m.owner }
  else
    methods = object.methods.map { |m| object.method(m) }.group_by { |m| m.owner }
    rejected = [ Kernel, Object, BasicObject ]
    if method == false || method == nil
      methods.delete_if { |key| rejected.include?(key) }
    end
  end
  What.pager { |io| describe(methods, io) }
end

.describe(methods, io) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/what.rb', line 125

def What.describe(methods, io)
  first = true
  methods.each do |k, ms|
    ms = ms.sort_by { |m| m.name }
    io.puts unless first
    first = false
    io.puts "#{io.green(io.bold(k.to_s))} (#{io.green(k.class.to_s)})"
    ms.each do |m|
      prefix = (UnboundMethod === m) ? "#" : ""
      io.print "    #{io.blue(io.bold(prefix + m.name.to_s))}"
      if m.arity == -1 && m.parameters.empty?
        io.print "(...)"
      elsif m.arity != 0
        param_name = "a"
        parms = m.parameters.map do |type, name|
          if name.nil?
            name = param_name
            param_name = param_name.next
          end
          case type
          when :opt
            "#{name} = ?"
          when :rest
            "*#{name}"
          when :block
            "&#{name}"
          else
            "name"
          end
        end
        io.print "(" + parms.join(", ") + ((m.arity < 0) ? ", ...)" : ")")
      end
      if m.respond_to?(:super_method) && sm = m.super_method
        io.puts ", overrides #{sm}"
      end
      line_feeded = false
      if s = m.source_location
        io.print " #{s[0]}:#{s[1]}"
        begin
          lns = IO.read(s[0]).lines.to_a
          ln = s[1]-1
          found = false
          while ln >= 0 && ln >= s[1]-6 && !found
            endline = ln
            while lns[ln] =~ /^\s*#/
              beginline = ln
              found = true
              ln -= 1
            end
            if found
              doc = lns[beginline..endline]
              indent = doc.map { |ln| ln[/^\s*/].size }.min
              io.puts
              io.puts doc.map { |ln| "        " + io.green(ln[indent..-1].chomp) }
              line_feeded = true
            end
            ln -= 1
          end
        rescue Errno::ENOENT
        end
      end
      io.puts unless line_feeded
    end
  end
  nil
end

.instance_what?(klass, method = nil) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/what.rb', line 97

def What.instance_what?(klass, method = nil)
  methods = []
  if Symbol === method
    methods = [klass.instance_method(method)].group_by { |m| m.owner }
  else
    methods = klass.instance_methods.map { |m| klass.instance_method(m) }.group_by { |m| m.owner }
    rejected = [ Kernel, Object, BasicObject ]
    if method == false || method == nil
      methods.delete_if { |key| rejected.include?(key) }
    end
  end
  What.pager { |io| describe(methods, io) }
end

.pagerObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/what.rb', line 76

def What.pager
  env_pager = ENV["PAGER"]
  if pager = env_pager || where("less") || where("more")
    pager.tr!('/','\\')
    args = []
    args << "-r" if pager =~ /\bless\b/
    File.popen([pager, *args], "w") do |io|
      if (pager =~ /more\.com/i && ENV["ConEmuANSI"] == "ON") || args.include?("-r")
        io.extend(Colors)
      else
        io.extend(NoColors)
      end
      yield(io)
    end
  else
    io = STDOUT.dup
    io.extend(NoColors)
    yield(io)
  end
end

.where(program) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/what.rb', line 57

def What.where(program)
  if search_path = ENV["PATH"]
    paths = search_path.split(File::PATH_SEPARATOR)
    paths.each do |path|
      path.tr!('\\','/')
      candidate = File.join(path, program)
      return candidate if File.executable?(candidate)
      if pathext = ENV["PATHEXT"]
        pathexts = pathext.split(File::PATH_SEPARATOR)
        pathexts.each do |ext|
          candidate = File.join(path, program + ext)
          return candidate if File.executable?(candidate)
        end
      end
    end
  end
  nil
end