completion_proc=
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378 (0)
- 1_9_2_180 (1)
- 1_9_3_125 (38)
- 1_9_3_392 (0)
- 2_1_10 (-1)
- 2_2_9 (0)
- 2_4_6 (0)
- 2_5_5 (0)
- 2_6_3 (0)
- What's this?
completion_proc=(p1)
public
Specifies a Proc object proc to determine completion behavior. It should take input string and return an array of completion candidates.
The default completion is used if proc is nil.
The String that is passed to the Proc depends on the Readline.completer_word_break_characters property. By default the word under the cursor is passed to the Proc. For example, if the input is “foo bar” then only “bar” would be passed to the completion Proc.
Upon successful completion the Readline.completion_append_character will be appended to the input so the user can start working on their next argument.
Examples
Completion for a Static List
require 'readline' LIST = [ 'search', 'download', 'open', 'help', 'history', 'quit', 'url', 'next', 'clear', 'prev', 'past' ].sort comp = proc { |s| LIST.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = " " Readline.completion_proc = comp while line = Readline.readline('> ', true) p line end
Completion For Directory Contents
require 'readline' Readline.completion_append_character = " " Readline.completion_proc = Proc.new do |str| Dir[str+'*'].grep(/^#{Regexp.escape(str)}/) end while line = Readline.readline('> ', true) p line end
Autocomplete strategies
When working with auto-complete there are some strategies that work well. To get some ideas you can take a look at the completion.rb file for irb.
The common strategy is to take a list of possible completions and filter it down to those completions that start with the user input. In the above examples Enumerator.grep is used. The input is escaped to prevent Regexp special characters from interfering with the matching.
It may also be helpful to use the Abbrev library to generate completions.
Raises ArgumentError if proc does not respond to the call method.