Skip to content

Instantly share code, notes, and snippets.

@alebaffa
Created March 10, 2014 22:01
Show Gist options
  • Save alebaffa/9475390 to your computer and use it in GitHub Desktop.
Save alebaffa/9475390 to your computer and use it in GitHub Desktop.
String class does not call to_s when it is representing its own objects. So Kernel#puts which generally calls an objects to_s method does not do so when the object inherits from String.
class UnpredictableString
def initialize string
@self_badname = string
end
def scramble
@self_badname.split(//).shuffle.join
end
def to_s
scramble
end
end
my_unpredictable = UnpredictableString.new('This is unpredictable')
5.times do
puts my_unpredictable #if UnpredictableString inherits from String the puts returns nil.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment