January 2024
Intermediate to advanced
718 pages
20h 15m
English
While we’re talking about classes in Ruby, we feel like we should at least mention one of the most unique features of Ruby’s class structure: the ability to reopen a class definition and add new methods or variables to it at any time, even classes that are part of the third-party tools or the standard library.
In other words, if you have something like this in Ruby:
| | class Book |
| | attr_accessor :title |
| | |
| | # and a bunch of other stuff |
| | end |
Later, you can do this:
| | class Book |
| | def uppercase_title |
| | title.upcase |
| | end |
| | end |
If you declare class Book and a Book class already exists, Ruby won’t give an error, and the new definitions in the second declaration will be added to the existing class. This is true even ...
Read now
Unlock full access