[MacRuby-devel] [MacRuby] #423: #respond_to? returns true, actual method call raises NoMethodError

MacRuby ruby-noreply at macosforge.org
Tue Feb 23 04:16:20 PST 2010


#423: #respond_to? returns true, actual method call raises NoMethodError
---------------------------------+------------------------------------------
 Reporter:  ecin@…               |       Owner:  martinlagardette@…        
     Type:  enhancement          |      Status:  new                       
 Priority:  minor                |   Milestone:                            
Component:  MacRuby              |    Keywords:                            
---------------------------------+------------------------------------------
Changes (by martinlagardette@…):

  * priority:  major => minor
  * type:  defect => enhancement


Old description:

> framework 'Cocoa'
>
> a = NSMetadataQuery.new
>
> a.respond_to? :setPredicate # => true
>
> a.setPredicate # => NoMethodError: undefined method `setPredicate' for
> #<NSMetadataQuery:0x2002a1e20>
>
> a.setPredicate "a" # => RuntimeError: NSInvalidArgumentException:
> -[NSCFString generateMetadataDescription]: unrecognized selector sent to
> instance 0x2002309c0

New description:

 {{{
 #!ruby
 framework 'Cocoa'

 a = NSMetadataQuery.new
 a.respond_to? :setPredicate # => true
 a.setPredicate # => NoMethodError: undefined method `setPredicate' for
 #<NSMetadataQuery:0x2002a1e20>
 a.setPredicate "a" # => RuntimeError: NSInvalidArgumentException:
 -[NSCFString generateMetadataDescription]: unrecognized selector sent to
 instance 0x2002309c0
 }}}

--

Comment:

 Hi!

 Thanks for the report.

 However, the `undefined method 'setPredicate'` is raised because
 `setPredicate` (without argument) doesn't exist.

 On the other hand, the `RuntimeError` is raised because, as the error
 says, you are sending an invalid argument (`NSInvalidArgumentException`)
 to `setPredicate:`. `NSString`s are not valid parameters to
 `setPredicate:`.[[BR]]
 As you can see, `setPredicate:` is trying to send
 `generateMetadataDescription` to `NSString`, which is why the error says
 `unrecognized selector sent to instance`. It is not targeted to the
 `NSMetadataQuery`, but to the `NSString` you sent as a parameter.

 Here is a correct way of using `setPredicate:`:
 {{{
 #!ruby
 framework 'Cocoa'

 q = NSMetadataQuery.new
 pre = NSPredicate.predicateWithFormat("grade = '7'", nil)
 p q.respond_to? :setPredicate # => true
 p q.setPredicate pre # => #<NSMetadataQuery:0x200230fe0>
 }}}

 However, I will try to see if we can change the `undefined method` into
 something like `missing parameter` or `valid candidates` etc. :-)

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/423#comment:2>
MacRuby <http://macruby.org/>



More information about the MacRuby-devel mailing list