Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class String
- def tsub subs
- [].tap do |item|
- self.split("%_").each do|elem|
- match = elem.match(/^\d/)
- unless match.nil?
- (0..match.length-1).each do |i|
- index = (match[i]).to_i
- #puts "index: #{index} match #{match[i]} subs: #{subs[index]}"
- unless index < 0 or subs[index].nil?
- elem.gsub! match[i], subs[index]
- else
- elem = "%_#{elem}"
- end
- end
- end
- item << elem
- end
- end.join ""
- end
- end
- puts "original"
- o = "Hello, %_0. Sup? %_1"
- puts o
- puts "interpolated"
- puts o.tsub ["Andrew", "Bye"]
- puts "original"
- y = "%_0 Hello, %_0. Sup? %_1"
- puts y
- puts "interpolated"
- puts y.tsub ["Andrew", "Bye"]
- puts "I work %_0 way %_1".tsub ["this", "as well"]
- 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