This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Listing an Object's Methods
CodeExpectedActual
Object.methods
["name", "private_class_method", "object_id", "new", ["to_a", "respond_to?", "ancestors", "module_eval", "const_missing", "type", "protected_methods", "instance_methods", "public_method_defined?", "superclass", "eql?", "instance_variable_set", "const_get", "is_a?", "autoload", "hash", "send", "to_s", "class_eval", "class_variables", "class", "instance_method", "public_instance_methods", "tainted?", "private_methods", "private_method_defined?", "__send__", "included_modules", "untaint", "const_set", "id", "inspect", "instance_eval", "clone", "public_methods", "protected_instance_methods", "protected_method_defined?", "extend", "freeze", "autoload?", "display", "public_class_method", "allocate", "__id__", "<=>", "<", "methods", "method", "==", "===", ">", "nil?", "dup", "instance_variables", "include?", "private_instance_methods", ">=", "instance_of?", "const_defined?", "<=", "name", "private_class_method", "new", "object_id", "=~", "singleton_methods", "method_defined?", "equal?", "taint", "constants", "frozen?", "instance_variable_get", "kind_of?"]
Object.singleton_methods
[] []
Fixnum.singleton_methods
["induced_from"] ["induced_from"]
class MyClass
  def MyClass.my_singleton_method
 end
  def my_instance_method
  end
end
MyClass.singleton_methods
["my_singleton_method"] ["my_singleton_method"]
''.methods == String.instance_methods
true true
Object.methods.sort
["<", "<=", "<=>", "==", "===", "=~", ">", ">=", ["<", "<=", "<=>", "==", "===", "=~", ">", ">=", "__id__", "__send__", "allocate", "ancestors", "autoload", "autoload?", "class", "class_eval", "class_variables", "clone", "const_defined?", "const_get", "const_missing", "const_set", "constants", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "include?", "included_modules", "inspect", "instance_eval", "instance_method", "instance_methods", "instance_of?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "method", "method_defined?", "methods", "module_eval", "name", "new", "nil?", "object_id", "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?", "to_a", "to_s", "type", "untaint"]
MyClass.method_defined? :my_instance_method
true true
MyClass.new.respond_to? :my_instance_method
true true
MyClass.respond_to? :my_instance_method
false false
MyClass.respond_to? :my_singleton_method
true true
require 'irb/completion'
#Depending on your system, you may also have to add the following line:
IRB.conf[:use_readline] = true
String.private_instance_methods.sort
["Array", "Float", "Integer", "String", "`", "abort", "at_exit", ["Array", "Float", "Integer", "String", "`", "abort", "at_exit", "autoload", "autoload?", "binding", "block_given?", "callcc", "caller", "catch", "eval", "exec", "exit", "exit!", "fail", "fork", "format", "getc", "gets", "global_variables", "initialize", "initialize_copy", "irb_binding", "iterator?", "lambda", "load", "local_variables", "loop", "method_missing", "open", "p", "print", "printf", "proc", "putc", "puts", "raise", "rand", "readline", "readlines", "remove_instance_variable", "require", "set_trace_func", "singleton_method_added", "singleton_method_removed", "singleton_method_undefined", "sleep", "sprintf", "srand", "syscall", "system", "test", "throw", "timeout", "trace_var", "trap", "untrace_var", "warn"]
String.new.respond_to? :autoload?
false false
String.new.autoload?
NoMethodError: private method `autoload?' called for "":String
...
NoMethodError: private method `autoload?' called for "":String
	from (irb):22