From: Steve Tu Date: 2013-02-02T00:06:39+09:00 Subject: Re: Novice: self, @self.respond_to? and dynamically built methods unknown wrote in post #1094752: > Am 01.02.2013 12:36, schrieb Steve Tu: >> >> >> if self.respond_to?(method) >> What I want is to dynamically build a method name, then see if the >> object (in this case 'self' or the current module) supports that method >> and if so to then execute that method with any optional arguments. > > To me, this does not sound like a use case for metaprogramming. > Why don't you use a method that takes the section as an argument? > > > But to your question: I assume you are talking about 'global' methods? > If so, I cannot reproduce your problem: > > > 1.9.3p374 :001 > def test_method; puts 'test'; end > => nil > 1.9.3p374 :002 > self.methods.include?(:test_method) > => true > 1.9.3p374 :003 > test_method > test > => nil > 1.9.3p374 :004 > self.send(:test_method) > test > => nil > 1.9.3p374 :005 > self.respond_to?(:test_method) > => true The code I have is now split into separate files that are 'load' or 'require'd. The argument test file contains: #----------------------------------------------------------------------- # F O R M A L A R G U M E N T S #----------------------------------------------------------------------- def arguments_test require 'getoptlong' # The parameters can be in any order STDOUT.puts "Arguments "+ARGV.length.to_s unless ARGV.length >= 1 `zenity --info --title="Usage ... " --text="Usage: ./ptutorial.rb -s\"Subject\" -o\"Options\""` exit end subject = option = '' # specify the options we accept and initialize # the option parser opts = GetoptLong.new( [ "--subject", "-s", GetoptLong::REQUIRED_ARGUMENT ], [ "--option", "-o", GetoptLong::OPTIONAL_ARGUMENT ], ) # process the parsed options opts.each do |opt, arg| case opt when '--subject' subject = arg when '--option' option = arg end end STDOUT.puts "(#{self}) - (#{self.to_s}) Supports (#{self.methods})" STDOUT.puts "No Class Supports (#{methods})" method = "#{subject.to_s.chomp.downcase}_test" if self.respond_to?(method) self.send(method) else if self.respond_to?(method,TRUE) self.send(method,option) else `zenity --info --title="Arguments ... " --text="Args: (#{@self}).... Does Not Support #{method}"` end end `zenity --info --title="Arguments ... " --text="Args: Subject (#{subject}) Option (#{option})"` end What I now end up with is the 'private' respond_to? test working - which is one point I don't understand (ie what determines a method's visibility - I thought it had to be explicitly defined or was assumed as public?). That then lead onto the question re how to determine the footprint of the method being called, as the code above works if the method passed accepts a single parameter. If the method accepts o parameters then the code fails anyway - and hence the question re whether it is simply then better to just attempt the method call and trap the errors thrown, rather than trying to see if the method is supported in the first place. Am I making any sense at all? -- Posted via http://www.ruby-forum.com/.