module What

Constants

VERSION

Public Class Methods

about(object, method = nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
pager() { |io| ... } click to toggle source
# 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) click to toggle source
# 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