среда, 26 ноября 2008 г.

Просмотр методов класса в ruby

Любой руби-класс наследуется от Object и умеет отвечать на запрос "methods" выдавая список своих методов. Я часто использую такой список при отладке. Однако, он сильно замусорен методами самого Object:

>> Object.new.methods
=> ["inspect", "to_yaml_style", "to_query", "require_or_load", "tap", "silence_stderr", "stubba_object", "clone", "to_matcher", "copy_instance_variables_from", "decode64", "public_methods", "__send__", "object_id", "instance_variable_defined?", "require_library_or_gem", "equal?", "freeze", "daemonize", "extend", "subclasses_of", "debugger", "send", "methods", "to_yaml_properties", "acts_like?", "blank?", "silence_stream", "to_json", "hash", "decode_b", "method_exists?", "dup", "to_enum", "dclone", "instance_variables", "eql?", "reset_mocha", "instance_eval", "id", "returning", "breakpoint", "singleton_methods", "load", "taint", "unloadable", "send!", "duplicable?", "stubs", "instance_variable_get", "frozen?", "enum_for", "display", "instance_of?", "method", "to_a", "__is_a__", "stubba_method", "`", "instance_exec", "type", "instance_values", "require_dependency", "protected_methods", "==", "suppress", "require", "===", "with_options", "instance_variable_set", "respond_to?", "kind_of?", "remove_subclasses_of", "extended_by", "expects", "to_s", "taguri", "gem", "silence_warnings", "enable_warnings", "to_param", "class", "encode64", "require_association", "instance_variable_names", "private_methods", "=~", "tainted?", "__id__", "taguri=", "untaint", "nil?", "mocha_inspect", "b64encode", "mocha", "is_a?", "to_yaml", "extend_with_included_modules_from", "__metaclass__"]


Найдите десять отличий (мне захотелось узнать что умеет класс Exception):

>> Exception.new.methods
=> ["inspect", "to_yaml_style", "to_query", "require_or_load", "tap", "silence_stderr", "stubba_object", "clone", "to_matcher", "copy_instance_variables_from", "decode64", "copy_blame!", "public_methods", "__send__", "object_id", "instance_variable_defined?", "require_library_or_gem", "equal?", "freeze", "daemonize", "extend", "subclasses_of", "debugger", "send", "methods", "to_yaml_properties", "acts_like?", "blank?", "silence_stream", "exception", "to_json", "hash", "decode_b", "framework_backtrace", "method_exists?", "dup", "to_enum", "dclone", "blame_file!", "instance_variables", "eql?", "reset_mocha", "id", "instance_eval", "breakpoint", "clean_backtrace", "returning", "singleton_methods", "message", "load", "taint", "unloadable", "send!", "to_str", "duplicable?", "stubs", "instance_variable_get", "frozen?", "enum_for", "display", "instance_of?", "clean_message", "method", "to_a", "blamed_files", "__is_a__", "stubba_method", "`", "instance_exec", "type", "instance_values", "application_backtrace", "require_dependency", "protected_methods", "==", "suppress", "backtrace", "require", "===", "with_options", "instance_variable_set", "respond_to?", "kind_of?", "remove_subclasses_of", "extended_by", "expects", "taguri", "describe_blame", "to_s", "gem", "silence_warnings", "enable_warnings", "to_param", "class", "encode64", "require_association", "instance_variable_names", "private_methods", "=~", "tainted?", "__id__", "taguri=", "set_backtrace", "untaint", "nil?", "mocha_inspect", "b64encode", "mocha", "is_a?", "to_yaml", "extend_with_included_modules_from", "__metaclass__"]


А теперь вспомним, что массивы руби перегружают оператор вычитания:
>> Exception.new.methods - Object.methods
=> ["copy_blame!", "exception", "framework_backtrace", "blame_file!", "clean_backtrace", "message", "to_str", "clean_message", "blamed_files", "application_backtrace", "backtrace", "describe_blame", "set_backtrace"]


Ну как, удобнее? :)

Я использовал Object.methods, а не Object.new.methods, потому что Object.methods это методы экземпляра объекта Class... который сам унаследован от Object.

Комментариев нет: