Justin Collins
1/29/2009 5:11:00 AM
Albert Schlef wrote:
> I'm looking for some study aid. I want to understand Ruby's object model
> better.
>
> Is there some tool that takes some object as input and shows its
> inheritance chain, it's instance and class variables, the singleton
> classes and all?
>
> I've already seen some diagrams that explain how OOP work in Ruby, but
> I'd still like to see all this in my own eyes.
>
I think you want something more visual than this (?) but you can always
just ask Ruby itself:
irb(main):001:0> s = String.new
=> ""
irb(main):002:0> s.class
=> String
irb(main):003:0> s.class.ancestors
=> [String, Enumerable, Comparable, Object, Kernel]
irb(main):004:0> s.public_methods.sort
=> ["%", "*", "+", "<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=",
"[]", "[]=", "__id__", "__send__", "all?", "any?", "between?", "bytes",
"bytesize", "capitalize", "capitalize!", "casecmp", "center", "chars",
"chomp", "chomp!", "chop", "chop!", "class", "clone", "collect",
"concat", "count", "crypt", "cycle", "delete", "delete!", "detect",
"display", "downcase", "downcase!", "drop", "drop_while", "dump", "dup",
"each", "each_byte", "each_char", "each_cons", "each_line",
"each_slice", "each_with_index", "empty?", "end_with?", "entries",
"enum_cons", "enum_for", "enum_slice", "enum_with_index", "eql?",
"equal?", "extend", "find", "find_all", "find_index", "first", "freeze",
"frozen?", "grep", "group_by", "gsub", "gsub!", "hash", "hex", "id",
"include?", "index", "inject", "insert", "inspect", "instance_eval",
"instance_exec", "instance_of?", "instance_variable_defined?",
"instance_variable_get", "instance_variable_set", "instance_variables",
"intern", "is_a?", "kind_of?", "length", "lines", "ljust", "lstrip",
"lstrip!", "map", "match", "max", "max_by", "member?", "method",
"methods", "min", "min_by", "minmax", "minmax_by", "next", "next!",
"nil?", "none?", "object_id", "oct", "one?", "partition",
"private_methods", "protected_methods", "public_methods", "reduce",
"reject", "replace", "respond_to?", "reverse", "reverse!",
"reverse_each", "rindex", "rjust", "rpartition", "rstrip", "rstrip!",
"scan", "select", "send", "singleton_methods", "size", "slice",
"slice!", "sort", "sort_by", "split", "squeeze", "squeeze!",
"start_with?", "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum",
"swapcase", "swapcase!", "taint", "tainted?", "take", "take_while",
"tap", "test", "to_a", "to_enum", "to_f", "to_i", "to_s", "to_str",
"to_sym", "tr", "tr!", "tr_s", "tr_s!", "type", "unpack", "untaint",
"upcase", "upcase!", "upto", "zip"]
irb(main):005:0> s.instance_variables
=> []
irb(main):006:0> class << s
irb(main):007:1> def test
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> s.singleton_methods
=> ["test"]
-Justin