mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Comparing Class Hierarchy Relationships as a Ruby til.
This commit is contained in:
@@ -71,6 +71,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
### ruby
|
### ruby
|
||||||
|
|
||||||
- [Are They All True?](ruby/are-they-all-true.md)
|
- [Are They All True?](ruby/are-they-all-true.md)
|
||||||
|
- [Comparing Class Hierarchy Relationships](ruby/comparing-class-hierarchy-relationships.md)
|
||||||
- [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md)
|
- [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md)
|
||||||
- [Destructuring Arrays In Blocks](ruby/destructuring-arrays-in-blocks.md)
|
- [Destructuring Arrays In Blocks](ruby/destructuring-arrays-in-blocks.md)
|
||||||
- [Disassemble Some Codes](ruby/disassemble-some-codes.md)
|
- [Disassemble Some Codes](ruby/disassemble-some-codes.md)
|
||||||
|
|||||||
27
ruby/comparing-class-hierarchy-relationships.md
Normal file
27
ruby/comparing-class-hierarchy-relationships.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Comparing Class Hierarchy Relationships
|
||||||
|
|
||||||
|
The comparator methods (`<`,`>`, etc.) can be useful for a lot of things. In
|
||||||
|
Ruby, they can be used to compare classes in order to understand how they
|
||||||
|
relate to one another on the class hierarchy.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
# Fixnum is a subclass of Integer
|
||||||
|
> Fixnum < Integer
|
||||||
|
=> true
|
||||||
|
# Integer is not a subclass of Fixnum
|
||||||
|
> Integer < Fixnum
|
||||||
|
=> false
|
||||||
|
# Fixnum and String are not related to one another
|
||||||
|
> Fixnum < String
|
||||||
|
=> nil
|
||||||
|
```
|
||||||
|
|
||||||
|
The `<` operator will tell you if there is a subclass relationship. The `>`
|
||||||
|
operator will tell you if there is an ancestor relationship. When `nil`
|
||||||
|
results, it means the two classes do not have a direct relationship.
|
||||||
|
|
||||||
|
There are a few more of [these types of
|
||||||
|
operators](http://ruby-doc.org/core-2.2.2/Module.html#method-i-3C) on the
|
||||||
|
Module class.
|
||||||
|
|
||||||
|
[source](http://kerrizor.com/blog/2015/05/14/serendipity-and-ruby-objects/)
|
||||||
Reference in New Issue
Block a user