From: Sascha Abel Date: 2007-05-30T19:03:41+09:00 Subject: Re: #respond_to? not working for dynamically generated metho Maurice Gladwell wrote: > It seems Object#respond_to doesn't work for dynamically generated > methods: > > SomeRailsModel.respond_to? 'find_by_name' #=> false > > 1. Is there some way to *really* check if an object will respond to a > message, even if it will respond by a dynamically generated method? > > Right now, Object#respond_to? just doesn't do what it name promises: it > doesn't check if an object responds to a message :foo, only if the > object has a :foo method defined. > > Thanks, > Maurice B. Gladwell I think 'Object#respond_to?' just looks at a list of previously *defined* methods, as does 'method_defined?', it doesn't matter if they're defined dynamically at runtime or not. 'find_by_name' is not really a method in Rails, it's just the magic of method_missing that you can call it on SomeRailsModel. In order to find out which methods SomeRailsModel would respond to you'd have to write a method that looks at 'method_missing' and works out all the possibilities there. I'm by no means an expert (neither for Ruby nor Rails), but SomeRailsModel will respond to every 'find_by_foo' method, where foo is column of the underlying database, so your task seems way too big for such like me.. Sascha -- Posted via http://www.ruby-forum.com/.