Created
March 10, 2014 22:01
-
-
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.
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 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