# Howto tab completion

**URL:** <https://www.ruby-forum.com/t/howto-tab-completion/51038>\
**Category:** Ruby\
**Created:** [December 24, 2005, 8:56am UTC](https://www.ruby-forum.com/t/howto-tab-completion/51038 "2005-12-24T08:56:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_SentropieS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Michael\_SentropieS\_S](https://www.ruby-forum.com/u/Michael_SentropieS_S)\
**Post date:** [December 24, 2005, 8:56am UTC](https://www.ruby-forum.com/t/howto-tab-completion/51038/1 "2005-12-24T08:56:13Z")

</div>

Hello,

I use [1] to process a string from cmdline-user-input to call methods  
form a module namespace. It works for  
now, but there are later a lot of commands to input so i want  
tab-completion because iam lazy 🙂

module CmdsBase  
CmdsBase.cmd\_foo  
DoSomething.new  
end

…  
end

All methods with /^cmd\_(\w\*)$/ should be completeable from  
commandline.

Any ideas to solve this clean?

[1]

# snip:

def self.cmd  
begin

```
  line = gets
  args, i =[], -1
  line.scan(/\w*\s/) do |a|
    a.strip!
    meth = nil
    unless (i+=1).zero? and

    MyBiadjCmdlineCaller.methods.include?(a)
      break if i.zero?
      args.push(a)
    else
      meth = a
    end

    unless args.size.zero? # ignoring input with arguments yet
      MyBiadjCMDLine.ok?(MyBiadjCmdlineCaller.method(meth).call)
    end

  end
end while true

```

end

## So long

Michael ‘entropie’ Trommer; [http://ackro.org](http://ackro.org)

ruby -e “0.upto((a=‘njduspAhnbjm/dpn’).size-1){|x| a[x]-=1}; p  
‘mailto:’+a”

---

<div class="post-metadata">

**Author:** ![Michael\_SentropieS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Michael\_SentropieS\_S](https://www.ruby-forum.com/u/Michael_SentropieS_S)\
**Post date:** [December 24, 2005, 9:30pm UTC](https://www.ruby-forum.com/t/howto-tab-completion/51038/2 "2005-12-24T21:30:49Z")

</div>

Have you tried using the Readline module?

require “readline”  
$completions = [“cmd\_1”,“cmd\_42”,“cmd\_one”]  
Readline::completion\_proc = Proc.new{ |prefix| $completions if prefix  
=~ /cmd\_/ }  
0.upto(4) { line = Readline::readline( "Cmd? ", true ) }

You get the idea…

Daniel.

---

<div class="post-metadata">

**Author:** ![Michael\_SentropieS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Michael\_SentropieS\_S](https://www.ruby-forum.com/u/Michael_SentropieS_S)\
**Post date:** [December 27, 2005, 6:39pm UTC](https://www.ruby-forum.com/t/howto-tab-completion/51038/3 "2005-12-27T18:39:12Z")

</div>

- Daniel Calvelo (removed\_email\_address@domain.invalid) wrote:

> Have you tried using the Readline module?
> 
> require “readline”  
> $completions = [“cmd\_1”,“cmd\_42”,“cmd\_one”]  
> Readline::completion\_proc = Proc.new{ |prefix| $completions if prefix  
> =~ /cmd\_/ }  
> 0.upto(4) { line = Readline::readline( "Cmd? ", true ) }

thanks a lot, thats what i need!

## So long

Michael ‘entropie’ Trommer; [http://ackro.org](http://ackro.org)

ruby -e “0.upto((a=‘njduspAhnbjm/dpn’).size-1){|x| a[x]-=1}; p  
‘mailto:’+a”
