Studying the object model

A

Albert Schlef

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.
 
M

Michael Fellinger

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.

There is no interactive tool I know of, but read following:

http://jaoo.dk/ruby-cph-2008/file?path=/jaoo-ruby-cph-2008/slides/Dave_Metaprogramming.pdf

(it's free, unlike the screencast)

^ manveru
 
J

Justin Collins

Albert said:
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
 
R

Robert Klemme

2009/1/29 Justin Collins said:
I think you want something more visual than this (?) but you can always just
ask Ruby itself:

Here's an easy way to get a visual graph of the currently defined
class hierarchy:

ruby -r pp -e 'tree = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
ObjectSpace.each_object(Class) {|cl| tree[cl.superclass][cl] = tree[cl]}
pp tree[Object]'

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top