There have been many times while in irb or the rails console that I actually want to see what an object can do. This is a simple task as all you have to do is run “obj.methods” and you get the following output:
["postalCode", "methods", "require_library_or_gem", "instance_eval", "po", "decode_b", "dup", "daemonize", "to_yaml_properties", "postalCode=", "instance_variables", "copy_instance_variables_from", "taguri", "instance_of?", "extend", "eql?", "accountNo", "returning", "remove_subclasses_of", "pretty_print", "instance_exec", "taguri=", "hash", "id", "country", "singleton_methods", "bandwidth", "accountNo=", "to_yaml", "poc", "taint", "instance_variable_get", "frozen?", "local_methods", "bandwidth=", "country=", "kind_of?", "method", "pretty_print_instance_variables", "to_a", "pretty_inspect", "suppress", "dclone", "password", "userID", "display", "with_options", "type", "protected_methods", "password=", "load", "userID=", "nPANXX", "enable_warnings", "instance_variable_set", "nPANXX=", "is_a?", "state", "respond_to?", "to_s", "streetAddress", "this_method", "subclasses_of", "`", "object_id", "class", "unloadable", "state=", "private_methods", "==", "tainted?", "require", "streetAddress=", "serviceType", "id", "===", "ri", "b64encode", "untaint", "nil?", "silence_stderr", "helper", "encode64", "to_param", "serviceType=", "extend_with_included_modules_from", "inspect", "send", "silence_warnings", "city", "require_gem", "to_json", "decode64", "blank?", "=~", "clone", "public_methods", "to_yaml_style", "city=", "popName", "extended_by", "send", "freeze", "equal?", "silence_stream", "gem", "popName=", "pretty_print_cycle", "instance_values", "pretty_print_inspect"]
I guess this would be fine if you really wanted to see everything, but I find that more times than not, I just just want to see the methods added by the object’s class. I created a simple mixin and added it to my .irbrc:
1 2 3 4 5 6 |
class Object def local_methods m = methods - Object.new.methods m.sort end end |
["accountNo", "accountNo=", "bandwidth", "bandwidth=", "city", "city=", "country", "country=", "nPANXX", "nPANXX=", "password", "password=", "popName", "popName=", "postalCode", "postalCode=", "serviceType", "serviceType=", "state", "state=", "streetAddress", "streetAddress=", "userID", "userID="]
Now, I have easy access to capabilities of my object.