By Keith Connolly on November 03, 2010Get Reading
So in php you could use this cool method to check if other methods existed, namely, method_exists.
You'd use it something like this:
bool method_exists ( mixed $object , string $method_name )
So playing around in rails (as I often do) I was faced with a problem -- I needed method_exists. I tried doing things like method? checking class refs all that jazz, in the end it all seemed way too complicated. I felt like I was missing something. So after some looking, I found out that responds_to does what I needed.
You can use it something like:
if(respond_to?("myMethod")) myMethod args else p "myMethod not found\n" end
Cool? I know... but what about private methods?!
if(respond_to?("myMethod",true)) myMethod args else p "myMethod not found\n" end
Yup you just add some of that true magic and it forces it to check privates. Groovy.
I really like hearing from those who read my content, engauge with me via the comments on each post are by one of the many contact methods below.