From: Vicente Bosch Campos Date: 2011-11-15T19:33:55+09:00 Subject: Re: Autocompletion of commands & arguments --Apple-Mail-9--551353325 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On Nov 15, 2011, at 5:17 AM, Sophie wrote: > I want to build a Ruby app with the following broad properties >=20 > - Has some graph of objects, with a root R > - each object (including R) has some methods, m > - each m on each object has some args A > - each A has a set of valid candidates values, defined by method = candidates_A_m > - the objects have (string) names >=20 > I want to build a command-line interface which lets me >=20 > - start at the root R > - invoke methods by typing in a prefix and using TAB > - choose arg values by typing in object-name prefix and TAB > - getting autocompletion or suggestion-list whenever possible > - change my "working" location in the graph (like "cd") & continue >=20 > I have not used Ruby in some time. Am looking for any suggestions on = useful classes / libraries / methods / apps I should consider to make my = life easier :-) >=20 > Thanks! >=20 >=20 >=20 Hi Sophie, What would the relation between the objects be ? Named relations ? You could use an OpenStruct or a hash to hold the related objects and = then iterate through that list as you press tab. The hash key would be = the object name string you indicated in your email.=20 Once on a object if you want to retrieve the public methods you can: = object.public_methods which returns you an array with the method symbols = so you can iterate through it.=20 Once on a method you can find out the arity of it with "method.arity" = and find out the name of the parameters and if it is requiered with = "method.parameters" and call it with "method.call"=20 e.g. =46rom the pick axe: def m(a, b=3D1, *c, &d)=20 end=20 method(:m).parameters # =3D> [[:req, :a], [:opt, :b], [:rest, :c], = [:block, :d]] I would not try to replicate that functionality as luckily Ruby provides = us with great reflection methods. Regards, V. --Apple-Mail-9--551353325--