`
qzriso
  • 浏览: 243029 次
  • 性别: Icon_minigender_1
  • 来自: ph
社区版块
存档分类
最新评论

Object and Reflection

阅读更多

摘自: http://anw.stikipad.com/ocean/show/Object+and+Reflection

 

  • 看看 ruby 如何深入探討 object 的相關功能, “o” 表示某個 object instance
    • o.class 找到 object 的 class
    • o.methods 可以找到 methods
    • o.respond_to? 測試某個 method 是否存在
    • o.object_id 它是 object 的唯一 id
    • o.inspect 可以 dump object 內容
    • o.is_a?, o.kind_of?
      當物件的 class 是某個 class 或是某個父字輩 class 或某個 mix-in 模組?
    • o.instance_of?
      當物件的 class 是某個 class?
      ps. 這個好像跟 java 不一樣!
    • o.nil? 是否為 null object?
    • 若你還有進一步需要, 這裡有更多 methods 可以幫你, 這些是將 “Module” mix-in 進去, 可不會在 Object 上找到!
      • private_instance_methods
      • protected_instance_methods
      • public_instance_methods
      • private_class_methods
      • protected_class_methods
      • public_class_methods
      • singleton_methods
      • class_variables
      • ...
        
        no = 100
        puts no.class
        puts no.class.superclass
        
        puts no.is_a? Integer
        puts no.kind_of? Integer
        puts no.instance_of? Integer
        
        
        
        
        
  • 所有的 methods, 在 ruby 都可以用 send 呼叫
    
    "John Coltrane".send(:length)
    "Miles Davis".send("sub", /iles/, '.')
    
    
    
    
    
    • 這是 Proxy 的例子, 利用 ruby 在找不到 method 時一定會試著呼叫 method_missing 的機制就可完成.
      
      class Proxy
        def initialize(object)
          @object = object
        end
      
        def method_missing(symbol, *args)
          @object.send(symbol, *args)
        end
      end
      
      data = ["a", "b", "c"]
      proxy = Proxy.new(data)
      puts proxy.first # Outputs: "a" 
      
      
      
      
      
  • class 的繼承關係? 可以利用 Class#superclass 找到父親. 但 ruby 還可以利用 mix-in 功能將別的模組的變數及 methods 加進去. 這時你就可以用 Module#ancestors 找到父親及 mix-in 模組.
    
    a = 123
    klass = a.class
    while klass do
      print klass
      klass = klass.superclass
      print " < " if  klass
    end
    
    
    
    
    
  • 因為 class 也是 constant, 所以你可以用 Module#constants 找到所有的 class(可能會有點多)
    
    Module.constants.each { |x|
      puts x
    }
    
    
    
    
    
  • 查看所有活著的物件, 要用 ObjectSpace.each_object
    
    class Book
      attr_accessor :title
    end
    
    class Magzine < Book
    end
    
    b1 = Book.new
    b1.title ="A" 
    b2 = Book.new
    b2.title ="B" 
    m1 = Magzine.new
    m1.title ="C" 
    
    ObjectSpace.each_object(Book) { |x|
      puts x.inspect
    }
    
    
    
    
分享到:
评论

相关推荐

    Object Pascal Handbook,最新Ddelphi书籍,for XE7

    Object Pascal Handbook This is the book summary Table of Contents: Part I Chapter 1: Coding in Pascal ...Chapter 16: Reflection and Attributes Chapter 17: The TObject Class Chapter 18: RunTime Library

    Pro C# 7: With .NET and .NET Core

    Gain a solid foundation in object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL ...

    PHP Objects, Patterns, and Practice, 4th Edition

    This edition introduces new object relevant features such as traits, reflection extension additions, callable type hinting, improvements to exception handling, and many smaller language enhancements....

    C# 6.0 and the .NET 4.6 Framework

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    Csharp.6.0.and.the.NET.4.6.Framework.7th.Edition.1484213335.epub

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    PHP Objects, Patterns, and Practice(Apress,2016)

    It introduces key topics including class declaration, inheritance, reflection and much more. The next section is devoted to design patterns. It explains the principles that make patterns powerful. ...

    C# 6.0 and the .NET 4.6 Framework(7th).pdf 2016第7版pdf

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    C#6.0 and the .NET 5 Framework(Apress,7ed,2016)

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    Professional C# 7 and .NET Core 2.0

    Chapter 16 Reflection, Metadata, And Dynamic Programming Chapter 17 Managed And Unmanaged Memory Chapter 18 Visual Studio 2017 PART II: .NET Core and the Windows Runtime Chapter 19 Libraries, ...

    The.Dart.Programming.Language.03219277

    Dart is a class-based, object-oriented language that simplifies the development of structured modern apps, scales from small scripts to large applications, and can be compiled to JavaScript for use in...

    The Dart Programming Language

    Dart’s object model, in which everything is an object, even numbers and Boolean values How Dart programs are organized into modular libraries How Dart functions are structured, stored in variables, ...

    Learning ECMAScript 6(PACKT,2015)

    Moving on, it will teach you how to create reflection objects, use it to expose hidden object properties, and test the security of these objects. Next, the book provides use case scenarios of meta ...

    Reflection_and_Annotations

    在`Reflection_and_Annotations-master`这个压缩包中,可能包含了一个示例项目,演示了如何结合使用反射和注解来实现动态Dao。通过阅读和学习这个项目,你可以更深入地理解这两个Java特性在实际开发中的应用,并提升...

    Ajax for Web Application Developers(Ajax网站开发)

    Data Reflection Pattern An Overview Creating the Pattern Chapter 18. Interaction Patterns Creating a History with Cookies Drag and Drop Chapter 19. Usability Patterns Handling Feedback,...

    Agile Java Crafting Code with Test-Driven Development

    Contains detailed chapters on exceptions and logging, math, I/O, reflection, multithreading, and Swing Offers seamlessly-integrated explanations of Java 5.0's key innovations, from generics to ...

Global site tag (gtag.js) - Google Analytics