Skip to content

Instantly share code, notes, and snippets.

@liushooter
Created April 3, 2015 01:00
Show Gist options
  • Save liushooter/731d9e9275dc03e1069a to your computer and use it in GitHub Desktop.
Save liushooter/731d9e9275dc03e1069a to your computer and use it in GitHub Desktop.
💋 邹倩雯
class MagicString < String
def +@
upcase
end
def -@
downcase
end
def !
swapcase
end
# Do a ROT13 transformation - http://en.wikipedia.org/wiki/ROT13
def ~
tr 'A-Za-z', 'N-ZA-Mn-za-m'
end
end
str = MagicString.new("This is my string!")
p +str # => "THIS IS MY STRING!"
p !str # => "tHIS IS MY STRING!"
p (not str) # => "tHIS IS MY STRING!"
p ~str # => "Guvf vf zl fgevat!"
p +~str # => "GUVF VF ZL FGEVAT!"
p !(~str) # => "gUVF VF ZL FGEVAT!"
# http://www.rubyinside.com/rubys-unary-operators-and-how-to-redefine-their-functionality-5610.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment