Studying the object model


#1

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.


#2

The Pragmatic screencast - Episode 1: Objects and Classes ($5.00, 29
mins)does a good job explaining Ruby object model -

http://www.pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming

Hope that helps.

—

Satish T.
RubyLearning
Twitter: http://twitter.com/IndianGuru


#3

On Thu, Jan 29, 2009 at 7:44 AM, Albert S. removed_email_address@domain.invalid
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.

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


#4

2009/1/29 Justin C. removed_email_address@domain.invalid:

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:

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


#5

Albert S. 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