module Brice::Shortcuts::ObjectShortcuts

Public Instance Methods

aorta(obj = self, editor = nil) click to toggle source

Cf. <rubyforge.org/snippet/detail.php?type=snippet&id=22>

# File lib/brice/shortcuts.rb, line 130
def aorta(obj = self, editor = nil)
  tempfile = Tempfile.new('aorta')
  YAML.dump(obj, tempfile)
  tempfile.close

  if editor ||= File.which_command(EDITORS)
    system(editor, path = tempfile.path)
    return obj unless File.exists?(path)
  else
    warn 'No suitable editor found. Please specify.'
    return obj
  end

  content = YAML.load_file(path)
  tempfile.unlink
  content
end
cgrep(needle) click to toggle source
# File lib/brice/shortcuts.rb, line 95
def cgrep(needle)
  needle = %r{#{Regexp.escape(needle)}}i unless needle.is_a?(Regexp)
  klass = is_a?(Class) ? self : self.class
  res = []

  ObjectSpace.each_object(Class) { |obj|
    next unless obj <= klass

    name = obj.name
    next unless name =~ needle

    res.push(name.empty? ? obj.inspect : name)
  }

  res
end
mgrep(needle) click to toggle source
# File lib/brice/shortcuts.rb, line 112
def mgrep(needle)
  methods.grep(
    needle.is_a?(Regexp) ? needle : %r{#{Regexp.escape(needle)}}i
  )
end
po(obj = self) click to toggle source

Print object methods, sorted by name. (excluding methods that exist in the class Object)

# File lib/brice/shortcuts.rb, line 120
def po(obj = self)
  obj.methods.sort - Object.methods
end
poc(obj = self) click to toggle source

Print object constants, sorted by name.

# File lib/brice/shortcuts.rb, line 125
def poc(obj = self)
  obj.constants.sort if obj.respond_to?(:constants)
end