Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. # Add some methods to all strings.
  2. class String
  3. def says(something)
  4. @body = something
  5. rebuild
  6. end
  7.  
  8. def to(someone)
  9. @to = someone
  10. rebuild
  11. end
  12.  
  13. def in_anger
  14. @punctuation = "!!!"
  15. rebuild
  16. end
  17.  
  18. def rebuild
  19. @punctuation ||= "."
  20. @from ||= self.clone
  21. @body ||= ""
  22. @to ||= ""
  23.  
  24. self.replace(@body)
  25. self << ", " unless @body.empty? || @to.empty?
  26. self << @to + @punctuation
  27. self << " Sincerely, #{@from}." unless @from.empty?
  28. self
  29. end
  30. end
  31.  
  32. # Say some stuff!
  33. puts "Justin".says("Hello")
  34. puts "Justin".says("Good bye").to("Kevin")
  35. puts "All of Florida".says("Fuck off").to("hurricanes").in_anger
  36.  
  37. # Outputs:
  38. # Hello. Sincerely, Justin.
  39. # Good bye, Kevin. Sincerely, Justin.
  40. # Fuck off, hurricanes!!! Sincerely, All of Florida.
Add Comment
Please, Sign In to add comment