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