在python中可以通过环境变量PYTHONSTARTUP指定python启动时自动引入的文件, ruby中如何实现类似功能呢?
2条回答 默认 最新
iteye_304 2008-08-06 14:09关注在 $HOME 目录里创建一个 .irbrc 文件,然后在里面写任意 Ruby 代码即可。例如这是我的 .irbrc
[code="ruby"]
require 'rubygems'
require 'pp'
require 'irb/completion'
require 'irb/ext/save-history'$KCODE = 'u'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
IRB.conf[:PROMPT_MODE] = :SIMPLEJust for Rails...
if rails_env = ENV['RAILS_ENV']
rails_root = File.basename(Dir.pwd)
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:PROMPT_I => "#{rails_root}> ",
:PROMPT_S => "#{rails_root}* ",
:PROMPT_C => "#{rails_root}? ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :RAILS# Called after the irb session is initialized and Rails has
# been loaded (props: Mike Clark).
IRB.conf[:IRB_RC] = Proc.new do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.instance_eval { alias :[] :find }
end
enddef mp(message, color = nil)
case color
when 'red' : color = '31;1'
when 'green' : color = '32;1'
when 'yellow' : color = '33;1'
when 'blue' : color = '34;1'
when 'purple' : color = '35;1'
when 'sky' : color = '36;1'
else color = '36;1'
end
print "\e[#{color}m#{message}\e[0m\n"
endvim: set ft=ruby:
[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报