This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Finding the Class and Superclass of an Object | ||
|---|---|---|
| Code | Expected | Actual |
'a string'.class |
String | String |
'a string'.class.name |
"String" | "String" |
'a string'.class.superclass |
Object | Object |
String.superclass |
Object | Object |
String.class |
Class | Class |
String.class.superclass |
Module | Module |
'a string'.class.new |
"" | "" |
class Class
def hierarchy
(superclass ? superclass.hierarchy : []) << self
end
end
Array.hierarchy |
[Object, Array] | [Object, Array] |
class MyArray < Array end MyArray.hierarchy |
[Object, Array, MyArray] | [Object, Array, MyArray] |
String.superclass |
Object | Object |
String.ancestors |
[String, Enumerable, Comparable, Object, Kernel] | [String, Enumerable, Comparable, Object, Kernel] |
Array.ancestors |
[Array, Enumerable, Object, Kernel] | [Array, Enumerable, Object, Kernel] |
MyArray.ancestors |
[MyArray, Array, Enumerable, Object, Kernel] | [MyArray, Array, Enumerable, Object, Kernel] |
Object.ancestors |
[Object, Kernel] | [Object, Kernel] |
class MyClass end MyClass.ancestors |
[MyClass, Object, Kernel] | [MyClass, Object, Kernel] |