Methods Madness
Posted about 1 year ago
Ever call a method on a class in Rails or even just Ruby and got this:
NoMethodError: undefined method `never’ for Time:Class
Now you swear on everyone that means anything to you, that the method you are using exists. Sure it does! No, really we believe you, but lets ask Ruby since, after all she should know best right! Now before I continue I wouldn’t know about this if some time ago my co-worker, Crazy Chicken Fingers, would not have told me about the methods method. So lets see what happens:
irb(main):003:0> Time.methods => ["inspect", "pretty_print", "private_class_method", "const_missing", "clone", "method", "public_methods", "public_instance_methods", "instance_variable_defined?", "method_defined?", "superclass", "equal?", "freeze", "mktime", "included_modules", "const_get", "pretty_print_instance_variables", "methods", "respond_to?", "_load", "module_eval", "class_variables", "now", "dup", "protected_instance_methods", "instance_variables", "public_method_defined?", "__id__", "pretty_inspect", "eql?", "object_id", "const_set", "id", "at", "singleton_methods", "send", "class_eval", "taint", "utc", "frozen?", "instance_variable_get", "include?", "private_instance_methods", "__send__", "instance_of?", "private_method_defined?", "to_a", "name", "autoload", "type", "new", "<", "protected_methods", "instance_eval", "<=>", "display", "==", ">", "===", "instance_method", "gm", "instance_variable_set", "kind_of?", "extend", "protected_method_defined?", "const_defined?", ">=", "ancestors", "to_s", "<=", "public_class_method", "pretty_print_cycle", "allocate", "hash", "pretty_print_inspect", "class", "instance_methods", "tainted?", "=~", "private_methods", "class_variable_defined?", "nil?", "untaint", "local", "times", "constants", "is_a?", "autoload?"] irb(main):004:0>
Saweet! By the time I read through all of that I could have found the methods nicely formatted in the API, and my eyes will be burned up anyway. So lets add a little help using the pp helper. But first we need to require it in, unless you have it in your .irbrc file, and in that case why are you still reading?
irb(main):002:0> pp Time.methods ["inspect", "pretty_print", "private_class_method", "const_missing", "clone", "method", "public_methods", "public_instance_methods", "instance_variable_defined?", "method_defined?", "superclass", "equal?", "freeze", "mktime", "included_modules", "const_get", "pretty_print_instance_variables", "methods", "respond_to?", "_load", "module_eval", "class_variables", "now", "dup", "protected_instance_methods", "instance_variables", "public_method_defined?", "__id__", "pretty_inspect", "eql?", "object_id", "const_set", "id", "at", "singleton_methods", "send", "class_eval", "taint", "utc", "frozen?", "instance_variable_get", "include?", "private_instance_methods", "__send__", "instance_of?", "private_method_defined?", "to_a", "name", "autoload", "type", "new", "<", "protected_methods", "instance_eval", "<=>", "display", "==", ">", "===", "instance_method", "gm", "instance_variable_set", "kind_of?", "extend", "protected_method_defined?", "const_defined?", ">=", "ancestors", "to_s", "<=", "public_class_method", "pretty_print_cycle", "allocate", "hash", "pretty_print_inspect", "class", "instance_methods", "tainted?", "=~", "private_methods", "class_variable_defined?", "nil?", "untaint", "local", "times", "constants", "is_a?", "autoload?"] => nil irb(main):003:0>
Pretty cool eh? As I said a co-worker showed me the methods method some time ago and I have used it quite a bit since then. However, I often found myself having an idea of what functionality I’m searching for, therefore I can make an educated guess on what the method might be called. In this case this random list is still very inconvenient. So me being me I started looking for a method to sort the results, wouldn’t you know I found one called…SORT! Yes, amazing I know.
irb(main):004:0> pp Time.methods.sort ["<", "<=", "<=>", "==", "===", "=~", ">", ">=", "__id__", "__send__", "_load", "allocate", "ancestors", "at", "autoload", "autoload?", "class", "class_eval", "class_variable_defined?", "class_variables", "clone", "const_defined?", "const_get", "const_missing", "const_set", "constants", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "gm", "hash", "id", "include?", "included_modules", "inspect", "instance_eval", "instance_method", "instance_methods", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "local", "method", "method_defined?", "methods", "mktime", "module_eval", "name", "new", "nil?", "now", "object_id", "pretty_inspect", "pretty_print", "pretty_print_cycle", "pretty_print_inspect", "pretty_print_instance_variables", "private_class_method", "private_instance_methods", "private_method_defined?", "private_methods", "protected_instance_methods", "protected_method_defined?", "protected_methods", "public_class_method", "public_instance_methods", "public_method_defined?", "public_methods", "respond_to?", "send", "singleton_methods", "superclass", "taint", "tainted?", "times", "to_a", "to_s", "type", "untaint", "utc"] => nil
Now that is something useful! Can’t tell you how many times I have used this and realized the method really does exist, that is was just a spelling mistake on my part…very frustrating to say the least!
There is a great article on how to pimp out your irb here.
Why is it that I just can’t remember to close my damn tags! Is it just me or all programmers?
Sean



