Last active
October 20, 2019 14:39
-
-
Save havenwood/24458ee80d2bb8ed30d95724768fc47a to your computer and use it in GitHub Desktop.
An example using DelegateClass with core Classes. For the why, see: https://words.steveklabnik.com/beware-subclassing-ruby-core-classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FerretString < String | |
def to_s | |
'Successfully overrode ...' | |
end | |
end | |
class WombatString < DelegateClass(String) | |
def initialize | |
super('') | |
end | |
def to_s | |
'Successfully overrode ...' | |
end | |
end | |
ferret = FerretString.new | |
ferret << 'A ferret named "Mei" ...' | |
"#{ferret}" | |
#=> "A ferret named \"Mei\" ..." | |
wombat = WombatString.new | |
wombat << 'A wombat named "Zoe" ...' | |
"#{wombat}" | |
#=> "Successfully overrode ..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment