Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. class String
  2. def tsub subs
  3. [].tap do |item|
  4. self.split("%_").each do|elem|
  5. match = elem.match(/^\d/)
  6. unless match.nil?
  7. (0..match.length-1).each do |i|
  8. index = (match[i]).to_i
  9. #puts "index: #{index} match #{match[i]} subs: #{subs[index]}"
  10. unless index < 0 or subs[index].nil?
  11. elem.gsub! match[i], subs[index]
  12. else
  13. elem = "%_#{elem}"
  14. end
  15. end
  16. end
  17. item << elem
  18. end
  19. end.join ""
  20. end
  21. end
  22.  
  23. puts "original"
  24. o = "Hello, %_0. Sup? %_1"
  25. puts o
  26. puts "interpolated"
  27. puts o.tsub ["Andrew", "Bye"]
  28. puts "original"
  29. y = "%_0 Hello, %_0. Sup? %_1"
  30. puts y
  31. puts "interpolated"
  32. puts y.tsub ["Andrew", "Bye"]
  33.  
  34.  
  35.  
  36. puts "I work %_0 way %_1".tsub ["this", "as well"]
  37. puts "This %_-1 will %_0 fail %_6 as long as negative numbers will not be used".tsub ["not"]
Add Comment
Please, Sign In to add comment