class IRB::ExtendCommand::Ls

def self.transform_args(args)

def self.transform_args(args)
  if match = args&.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\n\z/)
    args = match[:args]
    "#{args}#{',' unless args.chomp.empty?} grep: /#{match[:grep]}/"
  else
    args
  end
end

def class_method_map(classes, dumped_mods)

def class_method_map(classes, dumped_mods)
  dumped_methods = Array.new
  classes.map do |mod|
    next if dumped_mods.include? mod
    dumped_mods << mod
    methods = mod.public_instance_methods(false).select do |method|
      if dumped_methods.include? method
        false
      else
        dumped_methods << method
        true
      end
    end
    [mod, methods]
  end.compact
end

def dump_methods(o, klass, obj)

def dump_methods(o, klass, obj)
  singleton_class = begin obj.singleton_class; rescue TypeError; nil end
  dumped_mods = Array.new
  ancestors = klass.ancestors
  ancestors = ancestors.reject { |c| c >= Object } if klass < Object
  singleton_ancestors = (singleton_class&.ancestors || []).reject { |c| c >= Class }
  # singleton_class' ancestors should be at the front
  maps = class_method_map(singleton_ancestors, dumped_mods) + class_method_map(ancestors, dumped_mods)
  maps.each do |mod, methods|
    name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods"
    o.dump(name, methods)
  end
end

def execute(*arg, grep: nil)

def execute(*arg, grep: nil)
  o = Output.new(grep: grep)
  obj    = arg.empty? ? irb_context.workspace.main : arg.first
  locals = arg.empty? ? irb_context.workspace.binding.local_variables : []
  klass  = (obj.class == Class || obj.class == Module ? obj : obj.class)
  o.dump("constants", obj.constants) if obj.respond_to?(:constants)
  dump_methods(o, klass, obj)
  o.dump("instance variables", obj.instance_variables)
  o.dump("class variables", klass.class_variables)
  o.dump("locals", locals)
  o.print_result
end