Ruby/Reflection/Class Reflection

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Call ancestors on a class name to see its inheritance hierarchy, like this:

puts Fixnum.ancestors



class attribute of string instance

x = "This is a test"
puts x.class



Reflection and Metaprogramming Introduction

Finding the Class and Superclass of an Object
"a string".class                                  # => String
"a string".class.name                             # => "String"
"a string".class.superclass                       # => Object
String.superclass                                 # => Object
String.class                                      # => Class
String.class.superclass                           # => Module
"a string".class.new                              # => ""



The class method supercedes Object"s deprecated method type, or gets the name of the class as a string with the name method

myString = "asdf"
myString.class.name # => "String"



The Object class has a class method that returns the name of an instance"s class

myString = "asdf"
myString.class # => String



To determine the name of the superclass

myString = "asdf"
myString.class.superclass # => Object
String.superclass # => Object