From: "ekkehard.horner" Date: 2007-03-26T01:40:05+09:00 Subject: Re: pointer to function With the great help from Paul and Brian I changed just one line of the run method of my CAPP class: def run( sFnc ) puts @name + '::run( "' + sFnc + '" )' if @Fncs.has_key?( sFnc ) # use send to call method whose name is contained in sFnc # todo_stack.push( "relation between string variable and symbol" ) send sFnc else puts 'no ' + sFnc + ' in: ' + @Fncs.keys.join( ' ' ) end end and got a script that allows me to call CAPP methods by name specified on the command line, even though I cheated a little bit by using the name == key of @Fncs hash instead of the second element of the contained array. To make myself familiar with the solutions to the 'pointer to function' problem provided by Paul and Brian, I added CAPP methods like: # just to have something to call def tell_it( sParm ) puts @name + '::tell_it( "' + sParm + '" ) called successfully.' end and - more important: # obj.send( aSymbol [, args ]* ) -> anObject # Invokes the method identified by aSymbol, passing it any arguments specified. # You can use __send__ if the name send clashes with an existing method in obj. # Paul: def run (m) ... send m ..... r.run "first" # Brian: some_object.send(:method, args...) # fncs = { 'frsFnc' => [ 'just tell "frsFnc"', :frsFnc ], } def sendSym methods = { 'sym0' => [ 'using :tell_it' , :tell_it ] , 'str0' => [ 'using "tell_it"', "tell_it" ] , } methods.each { |keyval| send keyval[ 1 ][ 1 ], keyval[ 1 ][ 0 ] } end # Does send 'symbolize' the string? todo: check # http://www.troubleshooters.com/codecorn/ruby/symbols.htm or: # obj.method( aSymbol ) -> aMethod # Looks up the named method as a receiver in obj, returning a Method object (or # raising NameError). The Method object acts as a closure in obj's object instance, # so instance variables and the value of self remain available def useCall methods = { '0' => [ 'using method( :tell_it )', method( :tell_it ) ] } methods.each { |keyval| keyval[ 1 ][ 1 ].call keyval[ 1 ][ 0 ] } end # I believe, that's the way to go Now I can start to (a) put an enhanced version of the CAPP class into a module (b) write a script that will create a new Ruby file (requiring/using this class CAPP to come) in the current directory, if called like ruby xplore.rb new resp. will add a new skeleton for method and a suitable entry in the @methods hash, if called like ruby xplore.rb add 'short description of ' (c) explore basic data types, looping, database work, ... with Ruby Thank you very much Paul and Brian. Your answers were exactly what I was looking for - and yes I did enjoy this. Ekkehard