Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active October 20, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/24458ee80d2bb8ed30d95724768fc47a to your computer and use it in GitHub Desktop.
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
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