Ruby的irb和Unix shell一样,通过定制可以提供更多特性与方便。
配置:
1. 安装以下gem: wirble, map_by_method, what_methods
2. 建立一个名叫_irbrc文件,内容如下:
# Compiled by Cookoo
# Reference:
# http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb
# http://pablotron.org/software/wirble/
require 'rubygems'
require 'map_by_method'
require 'what_methods'
require 'wirble'
require 'irb/completion'
IRB.conf[:AUTO_INDENT]=true
class Regexp
def show(a)
a =~ self ? "#{$`}<<#{$&}>>#{$'}" : "no match"
end
end
Wirble.init
Wirble.colorize unless Config::CONFIG['host_os'] == 'mswin32'
3. 该文件放置位置随意,建议在linux下放到home下。然后在linux和win32下设置环境变量IRBRC,指向该文件的路径(包括文件名本身)。同时win32下还需设置一个HOME环境变量,建议指向c:\Documents and Settings\your_account
----------------------------
搞好了吧?
SHOW TIME!
来体验一下多了哪些功能:
(*代表只限linux平台提供)
*1. 输出结果语法着色
2. 自动换行缩进
>>if i = 1
>> puts i
3. 按两次tab, 自动补全(这个其实win32下本来就有)
>> [].e #tab tab
[].each [].empty? [].equal?
[].each_index [].entries [].extend
[].each_with_index [].eql?
*4. 直接使用ri 'something'
5. 直接使用pp
6. 命令行提示符改成>>和输出=>对齐
7. what? 猜api专用:
>> 3.14.what? 3 #什么方法返回3?
3.14.to_int == 3
3.14.floor == 3
3.14.round == 3
3.14.to_i == 3
3.14.prec_i == 3
3.14.truncate == 3
=> ["to_int", "floor", "round", "to_i", "prec_i", "truncate"]
8. 动态map:
>> [1,2,3].map{|x| x.succ} #常规方式
=> [2, 3, 4]
>> [1,2,3].map_succ #快捷方式
=> [2, 3, 4]
在Rails console里看查询结果很好用
9. irb命令行历史记录:下次重开irb依然保留
10. 对象研究快捷方式
>> poc Math #展示常量
=> ["E", "PI"]
>> po Math #展示非继承自Object的方法
=> ["acos", "acosh", "asin", "asinh", "atan", "atan2", "atanh", "cos", "cosh", "
erf", "erfc", "exp", "frexp", "hypot", "ldexp", "log", "log10", "sin", "sinh", "
sqrt", "tan", "tanh"]
11. regexp研究快捷方式
>> /[a-z]+/.show "pi is 3.14"
=> "<<pi>> is 3.14"