Irb/TipsAndTricksだと全てのシンボルが補完候補として出てきてしまう。以下のようなスクリプト(irbri.rb)を用意して、
require 'irb/completion' module Kernel def r(arg) puts `ri #{arg}` end private :r end class Module def i(meth = nil) if meth puts `ri #{self}##{meth}` else puts `ri #{self}` end end def c(meth = nil) if meth puts `ri #{self}::#{meth}` else puts `ri #{self}` end end end RICompletionProc = proc{|input| bind = IRB.conf[:MAIN_CONTEXT].workspace.binding case input when /(\s*([A-Z].*)\.([ic])\s*):(.*)/ pre = $1 receiver = $2 ic = $3 meth = $4 ? /\A#{Regexp.quote($4)}/ : /./ #} begin if ic == 'i' candidates = eval("#{receiver}.instance_methods(false)", bind) else candidates = eval("#{receiver}.methods(false)", bind) + ['new'] end candidates = candidates.grep(meth) candidates.map{|s| pre + ':' + s } rescue Exception candidates = [] end when /([A-Z]\w+)#(\w*)/ #} klass = $1 meth = $2 ? /\A#{Regexp.quote($2)}/ : /./ candidates = eval("#{klass}.instance_methods", bind) p meth candidates = candidates.grep(meth) p candidates candidates.map{|s| klass + '#' + s } else IRB::InputCompletor::CompletionProc.call(input) end } Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{(" Readline.completion_proc = RICompletionProc
irb を実行すると、
$ irb -r irbri.rb irb(main):001:0>String.i:s[TAB] #<= ここでTABをうつ。 String.i:scan String.i:split String.i:strip! String.i:succ! String.i:size String.i:squeeze String.i:sub String.i:sum String.i:slice String.i:squeeze! String.i:sub! String.i:swapcase String.i:slice! String.i:strip String.i:succ String.i:swapcase! irb(main):001:0> String.i:sum ------------------------------------------------------------- String#sum str.sum(n=16) => integer ------------------------------------------------------------------------ Returns a basic _n_-bit checksum of the characters in _str_, where _n_ is the optional +Fixnum+ parameter, defaulting to 16. The result is simply the sum of the binary value of each character in _str_ modulo +2n - 1+. This is not a particularly good checksum. => nil irb(main):002:0>
とStringのインスタンスメソッドしか補完候補にならない。クラスメソッドは.cでok。
色々欠陥はあるけど。
最近のコメント