From: Dossy Date: 2002-07-14T04:15:24+09:00 Subject: Re: Dynamic Methods On 2002.07.14, David Alan Black wrote: > Hi -- > > On Sun, 14 Jul 2002, John wrote: > > > > In any case, what did you need this for? > > > > I assume you mean having the class respond to methods that match a regexp? > > > > So you can have a user input commands without having to type the whole > > command. Also for having aliases for commands without having to write each > > method to call the main method. > > > > class foo > > def /[ls|dir|directory]/ > > end > > def /desc.*/ > > end > > end > > > For aliasing, you can use, ummmm, alias :-) > > class Foo > def ls; end > alias :dir :ls > alias :directory :ls > end Or, as I said: class Foo def ls; end def describe; end def method_missing(*args) case args[0].to_s when /ls|dir|directory/ self.ls(*args[1..args.size - 1]) when /desc.*/ self.describe(*args[1..args.size - 1]) else raise NameError, "undefined method #{args[0].to_s}" end end end -- Dossy -- Dossy Shiobara mail: dossy@panoptic.com Panoptic Computer Network web: http://www.panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70)