改进工具irb
Posted by cnruby Tue, 23 Oct 2007 21:29:00 GMT
wget http://rubyforge.org/frs/download.php/17691/Text-1.1.2.gemgem install Text-1.1.2.gem
vi spell.rb
require 'rubygems'
require 'Text/Levenshtein'
class Object
alias_method :orig_method_missing, :method_missing
def method_missing(symbol, *args)
matches = []
self.methods.each do |method|
if (Text::Levenshtein::distance(method, symbol.to_s) == 1)
matches << method
end
end
case matches.length
when 0:
when 1:
method = matches[0] if
Gem::StreamUI.new($stdin, $stdout).ask_yes_no("Typo detected: #{symbol} -> #{matches[0]}, accept?", true)
else
method, idx = Gem::StreamUI.new($stdin, $stdout).choose_from_list("Typo detected, please choose a match or enter to ignore", matches << "ignore")
end
if method.nil? or method == 'ignore'
orig_method_missing(symbol, *args)
else
self.send(method, *args)
end
end
end
http://crafterm.net/blog/articles/search?q=irb
________________________________________________________________ 您只要点击下面图标,就可以把本文加入到您喜欢的公共收藏库中去。






![Validate my RSS feed [Valid RSS]](/blog1/files/valid-rss.png)




