开发工具 這年頭還有人用 .irbrc 嗎?

victor · 2013年09月22日 · 最后由 hbin 回复于 2013年09月22日 · 5186 次阅读

今天運行 rails c 的時候提示我 no such file to load -- wirble 才想起來當年我有訂製過 ~/.irbrc 這個文件。

不知道大家現在還有這麼玩的麼?

# encoding: utf-8
# Making .irbrc posted at http://www.tech-angels.fr/post/963080350/improve-irb-and-fix-it-on-mac-os-x
# Work with rails3
require 'rubygems'
# Rails3: be sure to include wirble, hirb, pasteboaRb and awesome_print in your Gemfile
require 'wirble'
require 'hirb'
require 'pasteboaRb' # pasteboaRb is a Ruby interface to Mac OS X's Pasteboard.
require 'irb/completion' # You can discover what methods can be called on an object by Double-TAB.
require 'ap' # Pretty print your Ruby objects with style -- in full color and with proper indentation

# load wirble
Wirble.init
Wirble.colorize
# load hirb
# hirb (active record output format in table)
Hirb::View.enable

ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[1;30m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
ANSI[:MAGENTA] = "\e[35m"
ANSI[:CYAN] = "\e[36m"
ANSI[:WHITE] = "\e[37m"

# IRB Options
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:EVAL_HISTORY] = 200


if defined?(Rails)
  require 'logger'
  Rails.logger=Logger.new(STDOUT)
  #IRB.conf[:USE_READLINE] = true
  ActiveRecord::Base.logger = Rails.logger

  # Display the RAILS ENV in the prompt
  # ie : [Development]>>
  IRB.conf[:PROMPT][:CUSTOM] = {
   :PROMPT_N => "[#{Rails.env.capitalize}]>> ",
   :PROMPT_I => "[#{Rails.env.capitalize}]>> ",
   :PROMPT_S => nil,
   :PROMPT_C => "?> ",
   :RETURN => "=> %s\n"
   }
  # Set default prompt
  IRB.conf[:PROMPT_MODE] = :CUSTOM
end

# We can also define convenient methods (credits go to thoughtbot)
def sql(query)
  ActiveRecord::Base.connection.select_all(query)
end

# annotate column names of an AR model
def show(obj)
  y(obj.send("column_names"))
end

# Add a method pm that shows every method on an object
# Pass a regex to filter these
def pm(obj, *options)
  methods = obj.methods
  methods -= Object.methods unless options.include? :more
  filter = options.select {|opt| opt.kind_of? Regexp}.first
  methods = methods.select {|name| name =~ filter} if filter

  data = methods.sort.collect do |name|
    method = obj.method(name)
    if method.arity == 0
      args = "()"
    elsif method.arity > 0
      n = method.arity
      args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
    elsif method.arity < 0
      n = -method.arity
      args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
    end
    klass = $1 if method.inspect =~ /Method: (.*?)#/
    [name.to_s, args, klass]
  end
  max_name = data.collect {|item| item[0].size}.max
  max_args = data.collect {|item| item[1].size}.max
  data.each do |item|
    print " #{ANSI[:YELLOW]}#{item[0].to_s.rjust(max_name)}#{ANSI[:RESET]}"
    print "#{ANSI[:BLUE]}#{item[1].ljust(max_args)}#{ANSI[:RESET]}"
    print " #{ANSI[:WHITE]}#{item[2]}#{ANSI[:RESET]}\n"
  end
  data.size
end

class Object
  def local_methods
    (methods - Object.instance_methods).sort
  end

  def to_pboard(pboard=:general)
    %x[printf %s "#{self.to_s}" | pbcopy -pboard #{pboard.to_s}]
    paste pboard
  end

  alias :to_pb :to_pboard
end

puts "****************************************************************************"
puts "* All systems are go wirble/hirb/pasteboaRb/auto-completion/awesome_print. *"
puts "* You can discover what methods can be called on an object by Double-TAB.  *"
puts "* You have some useful methods:                                            *"
puts "* sql(query), show(class.name), obj.local_methods, obj.to_pb, ap(obj)      *"
puts "* pm(obj, *options)                                                        *"
puts "* pm object, :more - shows all methods including base Object methods       *"
puts "* pm object, :more, /to/ - shows all methods filtered by regexp            *"
puts "*                                                                          *"
puts "* Example:                                                                 *"
puts "* Annotate column names of an AR model.                                    *"
puts "* >> show Role                                                             *"
puts "*                                                                          *"
puts "* Print role methods and copy them into Mac OS X’s Pasteboard.             *"
puts "* >> ap Role.first.local_methods.to_pb                                     *"
puts "*                                                                          *"
puts "* Print all of the methods for the object filtered by regexp.              *"
puts "* >> ap pm Role.first, /user/                                              *"
puts "*                                                                          *"
puts "* Import table() and view().                                               *"
puts "* >> extend Hirb::Console                                                  *"
puts "* >> urls = Url.all :limit=>2, :order=>'id DESC'; table urls               *"
puts "* >> table urls, :fields=>[:id, :name, :description]                       *"
puts "*                                                                          *"
puts "* You can also define convenient methods in ~/.irbrc file.                 *"
puts "****************************************************************************"

嗯,我创建了这个文件后 irb 才有历史记录。

#1 楼 @yanhao 能把你的文件貼出來嗎?想看看別人怎麼訂製的

~/.pryrc

begin
  require 'active_support/core_ext'
rescue LoadError
end

require 'matrix'
require 'prime'
INF = 1.0/0
class Range
  def method_missing *xs, &p
    to_a.send *xs, &p
  end
end

我的非常简单,只是配置了一下历史记录:

#history
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"

IRB.conf[:AUTO_INDENT] = true

不请自来

require 'irb/completion'
require 'irb/ext/save-history'
require 'rubygems'

IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:AUTO_INDENT] = true
需要 登录 后方可回复, 如果你还没有账号请 注册新账号