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.
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.