class RubyWho

@file @brief @author ongaeshi @date 2011/03/09

Constants

COLS
IGNORE

Public Class Methods

new(obj, io, filter_re, kind) click to toggle source
# File lib/rubywho.rb, line 48
def initialize(obj, io, filter_re, kind)
  @obj = obj
  @io = io
  @filter_re = filter_re.is_a?(String) ? Regexp.new(filter_re) : filter_re
  @kind = kind
end

Public Instance Methods

display(n = nil) click to toggle source
# File lib/rubywho.rb, line 65
def display(n = nil)
  obj_klass = @obj.is_a?(Module) ? @obj : @obj.class

  if singleton?
    limit(obj_klass.ancestors, n).each do |klass|
      @io.puts klass.to_s + '(%s)' % klass.class
      display_methods(klass.singleton_methods(false))
    end
  else
    limit(obj_klass.ancestors, n).each do |klass|
      @io.puts klass.to_s + '#'
      display_methods(klass.public_instance_methods(false))
    end
  end
end
obj_str() click to toggle source
# File lib/rubywho.rb, line 55
def obj_str
  klass = @obj.is_a?(Module) ? @obj : @obj.class
  
  if singleton?
    klass.to_s + '(%s)' % klass.class
  else
    klass.to_s + '#'
  end
end

Private Instance Methods

display_each_methods(methods, cols) click to toggle source
# File lib/rubywho.rb, line 99
def display_each_methods(methods, cols)
  buf = '| '
  methods.map(&:to_s).each do |m|
    next if @filter_re && @filter_re !~ m
    if (buf + m).length > cols
      @io.puts buf.sub(/\,\s*\Z/, '')
      buf = '| ' + m + ', '
    else
      buf << m << ', '
    end
  end
  @io.puts buf.sub(/\,\s*\Z/, '') unless buf == '| '
end
display_methods(methods) click to toggle source
# File lib/rubywho.rb, line 92
def display_methods(methods)
  alpha, op = methods.sort.partition{|m| m =~ /\A[_A-Za-z]/o }
  display_each_methods(alpha, COLS)
  display_each_methods(op, COLS)
  @io.puts 'v' + '-' * (COLS - 1)
end
limit(ancestors, n) click to toggle source
# File lib/rubywho.rb, line 87
def limit(ancestors, n)
  as = ancestors.reject{|a| IGNORE.include?(a) }
  n.nil? ? as : as[0, n]
end
singleton?() click to toggle source
# File lib/rubywho.rb, line 83
def singleton?
  @obj.is_a?(Module) && @kind != :instance || @kind == :singleton
end