ruby 对象模型

Posted by wxianfeng Mon, 07 Jan 2013 21:54:00 GMT

环境: mac 10.8 + ruby 1.9.2
ruby 对象模型非常重要, 不了解ruby对象模型, 不算真正掌握ruby, ruby 号称一切都是对象, why? 当你知道对象模型就清楚了.

说到ruby对象模型, 这张图是ruby基本的类关系.

class 表示实例关系(水平),super 表示继承关系(垂直),从图中可以看出

objmy 是 MyClass 的实例
MyClass 是 Class 的实例
Class 的父类是 Module
MyClass 的父类是 Object
Object 的父类是 BasicObject (ruby1.9)
MyClass include 模块M后,相当于在继承祖先链正上方继承该模块
Object include 模块Kernel
Module类的superclass 是Object
所有都可以在irb看到:

ruby-1.9.2-p290 :001 > MyClass = Class.new
 => MyClass 
ruby-1.9.2-p290 :002 > objmy = MyClass.new
 => #<MyClass:0x007f9ea1aba080> 
ruby-1.9.2-p290 :003 > objmy.class
 => MyClass 
ruby-1.9.2-p290 :004 > MyClass.class
 => Class 
ruby-1.9.2-p290 :005 > MyClass.superclass
 => Object 
ruby-1.9.2-p290 :006 > Class.class
 => Class 
ruby-1.9.2-p290 :007 > Class.superclass
 => Module 
ruby-1.9.2-p290 :008 > Module.class
 => Class 
ruby-1.9.2-p290 :009 > Module.superclass
 => Object 
ruby-1.9.2-p290 :010 > Object.class
 => Class 
ruby-1.9.2-p290 :011 > Object.superclass
 => BasicObject 
ruby-1.9.2-p290 :012 > BasicObject.class
 => Class 
ruby-1.9.2-p290 :013 > BasicObject.superclass
 => nil 

所以掌握了 Object,Module,Class 三者之间关系很重要, ruby 很多gem里的方法都是打开 Object来注入方法的.这样下面任何对象就都有该实例方法了.

demo:

#!/usr/bin/env ruby

class Object
        def foo
                p "im foo"
        end
end

MyClass = Class.new
objmy = MyClass.new
objmy.foo # im foo

有一个gem可以查看ruby对象模型, drx
安装:

>gem install drx
>brew install graphviz

使用:

wxianfeng-2:entos wangxianfeng$ irb
ruby-1.9.2-p290 :001 > require 'drx'
 => true 
ruby-1.9.2-p290 :002 > MyClass = Class.new
 => MyClass 
ruby-1.9.2-p290 :003 > objmy = MyClass.new
 => #<MyClass:0x007fcd1221b2e0> 
ruby-1.9.2-p290 :004 > objmy.see

出现下图:

这样就更加一目了然了.但是注意上图引入了eigenclass概念, 想了解 ruby 中方法查找的话, 就必须要知道了, 下回分解.

This entry was posted on Mon, 07 Jan 2013 21:54:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=258

Comments

  1. Avatar
    epibatiga ploia almost {{count}} years ago:

    Awesome issies here. I’m very glad to look your post.
    Thank you so much and I am looking forward tto contact you.

    Will youu kindly drop me a e-mail?

  2. Avatar
    epibatiga ploia almost {{count}} years ago:

    Awesome issues here. I’m vsry glaad to look your post. Thank you sso
    much and I am looking forward to contact you.

    Will you kindly drop me a e-mail?

  3. Avatar
    artmagazine.spb.ru almost {{count}} years ago:

    Thhis iis a great tip especially to those new to the blogosphere.
    Brieff but very precise info… Appreciate your sharing this one.
    A must read post!

  4. Avatar
    artmagazine.spb.ru almost {{count}} years ago:

    This is a grteat tip especially to those new to
    the blogosphere. Brioef but very precisse info… Appreciate your sharing
    this one. A must read post!

  5. This page really hhas all of the informjation I wanted concerning this subject and didn’t know who to
    ask.

  6. This page really has all of the information I wanted concerning this subject and didn’t know who tto ask.

  7. Avatar
    Minoan lines almost {{count}} years ago:

    Can I just say what a comfort to uncover a person that actually knows
    what they’re talking about online. You certainly understand how to bring a problem to light and make it important.
    More people should read thiis and understand this side of
    the story. I was surprised that yyou aren’t moe popular since you most certainly have the gift.

  8. Avatar
    Minoan lines almost {{count}} years ago:

    Can I just saay what a comfort to uncovver a person that actually
    knows what they’re talking about online. You certainly understand how to bring a problem to light and
    make it important. More people should read this and understand this side of
    thhe story. I was surprised that you aren’t more popular since you
    most certainly have the gift.

  9. Avatar
    greece ferry over {{count}} years ago:

    whoah thiis weblog iis excellent i like reading your
    posts. Keep up the great work! You already know, lots of persons aree looking round foor his
    info, you could help them greatly.

  10. Avatar
    greece ferry over {{count}} years ago:

    whoah this weblog is excellent i like reading your posts.
    Keep up thee great work! You already know, lots of persons aree looking round for this info, you
    could help them greatly.

  11. Avatar
    greece ferry over {{count}} years ago:

    Undeniably believe that thhat you said. Your favourite
    reason seemed to be at the internet the simplest thing to take
    into account of. I say to you,I certainly get iirked even as pdople consider concerns that they plainly don’t understand about.
    You controlled to hit the nail upon the top and defined out
    the whole thing without having side-effects , other folks can take a signal.
    Will likely bbe again to get more. Thank you

  12. Avatar
    greece ferry over {{count}} years ago:

    Undeniably believe that that you said. Your favourite reason sdemed to be at the
    internet the simplest thing to ake into account of.
    I say to you, I certainly get irked even aas people consider concerns that they plainly don’t understand about.
    You controlled to hit the nail upon thhe topp and defineed out the whole thing without having side-effects ,
    other folks can take a signal. Will likely bee again to get more.
    Thank you

Leave a comment