欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

irb默认只加载~/.irbrc下面的内容。

~/.irbrc里面输入下面的内容,rails console会自动加载当前项目根目录下的.irbrc文件,可以自定义调试辅助方法:

# requires and stuff go here

def load_irbrc(path)
  return if (path == ENV["HOME"]) || (path == '/')

  load_irbrc(File.dirname path)

  irbrc = File.join(path, ".irbrc")

  load irbrc if File.exists?(irbrc)
end

# other ruby code in your .irbrc

load_irbrc Dir.pwd # probably should stay at the bottom