# \[NOOB\] respond\_to? failing

**URL:** <https://www.ruby-forum.com/t/noob-respond-to-failing/192349>\
**Category:** Ruby\
**Created:** [July 28, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/noob-respond-to-failing/192349 "2010-07-28T20:38:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [July 28, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/noob-respond-to-failing/192349/1 "2010-07-28T20:38:18Z")

</div>

I’ve been checking respond\_to? before calling send() for ages but today  
its failing in a simple case.

I define a method outside of a class or module. send() works but  
respond\_to? fails. You could define a method in irb and check for  
respond\_to?

Where are these methods getting defined if not in Object?

## Here’s a snippet you can try:

#!/usr/bin/env ruby

def testme  
puts “inside testme”  
end

if respond\_to? :testme  
send :testme  
else  
puts “sorry”  
end

* * *

I just tried out “defined? :testme” and that works, but I’d still like  
to know which is better to use, and why respond\_to fails.

thx  
rahul

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [July 28, 2010, 10:27pm UTC](https://www.ruby-forum.com/t/noob-respond-to-failing/192349/2 "2010-07-28T22:27:52Z")

</div>

R… Kumar 1.9.1 OSX wrote:

> I’ve been checking respond\_to? before calling send() for ages but today  
> its failing in a simple case.
> 
> I define a method outside of a class or module. send() works but  
> respond\_to? fails. You could define a method in irb and check for  
> respond\_to?
> 
> Where are these methods getting defined if not in Object?
> 
> ## Here’s a snippet you can try:
> 
> #!/usr/bin/env ruby
> 
> def testme  
> puts “inside testme”  
> end
> 
> if respond\_to? :testme  
> send :testme  
> else  
> puts “sorry”  
> end
> 
> * * *
> 
> I just tried out “defined? :testme” and that works, but I’d still like  
> to know which is better to use, and why respond\_to fails.
> 
> thx  
> rahul

## That’s because the #testme method you defined is automatically made private and #respond\_to? checks only for public methods unless you instruct it otherwise by passing true as a second argument.

#ruby -v: ruby 1.9.1p429 (2010-07-02 revision 28523) [x86\_64-linux]  
irb(main):001:0\> def testme  
irb(main):002:1\> end  
=\> nil  
irb(main):003:0\> respond\_to?(:testme)  
=\> false  
irb(main):004:0\> respond\_to?(:testme, true)  
=\> true  
irb(main):005:0\> public\_methods.include?(:testme)  
=\> false  
irb(main):006:0\> private\_methods.include?(:testme)  
=\> true  
irb(main):007:0\> public :testme  
=\> Object  
irb(main):008:0\> respond\_to?(:testme)  
=\> true  
irb(main):009:0\>

* * *

Marvin

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [July 29, 2010, 5:34am UTC](https://www.ruby-forum.com/t/noob-respond-to-failing/192349/3 "2010-07-29T05:34:23Z")

</div>

Marvin GÃ¼lker wrote:

> That’s because the #testme method you defined is automatically made  
> private and #respond\_to? checks only for public methods unless you  
> instruct it otherwise by passing true as a second argument.
> 
> * * *
> 
> Marvin

Thanks a lot. Is this mentioned in some document or Pickaxe. I’d like to  
know why this is so, and what other rules there are such as this one.

thx, rahul.
