module Brice::Shortcuts

Convenient shortcut methods.

Set config.shortcuts.opt = { :object => false } to disable ObjectShortcuts, or config.shortcuts.opt = { :ri => false } to disable ri shortcuts.

Public Instance Methods

init(opt = {}) click to toggle source
# File lib/brice/shortcuts.rb, line 50
def init(opt = {})
  init_object if Brice.opt(opt, :object)
  init_ri     if Brice.opt(opt, :ri)
end
init_object() click to toggle source
# File lib/brice/shortcuts.rb, line 55
def init_object
  Object.send(:include, ObjectShortcuts)
end
init_ri() click to toggle source
# File lib/brice/shortcuts.rb, line 59
def init_ri
  Module.class_eval {
    def ri(*args)
      ri!('--no-pager', *args)
    end

    def ri!(*args)
      opts, args = args.partition { |arg| arg.to_s.start_with?('--') }

      args.empty? ? args << name : args.map! { |arg|
        arg, method = arg.to_s, nil

        delim = [['#', :instance_method], ['::', :method]].find { |i, m|
          match  = arg.sub!(/\A#{i}/, '')
          method = begin; send(m, arg); rescue NameError; end

          break i if match || method
        } or next arg

        "#{method && method.to_s[/\((\w+)\)/, 1] || name}#{delim}#{arg}"
      }

      system('ri', *opts.concat(args))
    end
  }

  instance_eval {
    def ri(*args);  Kernel.ri(*args);  end
    def ri!(*args); Kernel.ri!(*args); end
  }
end
ri(*args) click to toggle source
# File lib/brice/shortcuts.rb, line 61
def ri(*args)
  ri!('--no-pager', *args)
end
ri!(*args) click to toggle source
# File lib/brice/shortcuts.rb, line 65
def ri!(*args)
  opts, args = args.partition { |arg| arg.to_s.start_with?('--') }

  args.empty? ? args << name : args.map! { |arg|
    arg, method = arg.to_s, nil

    delim = [['#', :instance_method], ['::', :method]].find { |i, m|
      match  = arg.sub!(/\A#{i}/, '')
      method = begin; send(m, arg); rescue NameError; end

      break i if match || method
    } or next arg

    "#{method && method.to_s[/\((\w+)\)/, 1] || name}#{delim}#{arg}"
  }

  system('ri', *opts.concat(args))
end