PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/string-sub-gsub.rb

http://github.com/runpaint/read-ruby
Ruby | 12 lines | 9 code | 0 blank | 3 comment | 0 complexity | f2c29b738c0f6d1bcb7fa365b1e74870 MD5 | raw file
  1. alphabet = [*?a..?f].join
  2. alphabet.sub(/U/, 'you?') #=> "abcdef"
  3. alphabet.gsub(/[b-e]/){|match| "<U+#{'%.4X' % match.ord}>"}
  4. #=> "a<U+0062><U+0063><U+0064><U+0065>f"
  5. alphabet.sub!(?V, 'for Vendetta') #=> nil
  6. '3 + -4'.sub(/(?<a>\d+) \+ -(?<b>\d+)/,
  7. "== \\k<a> - \\k<b>")
  8. #=> "== 3 - 4"
  9. subs = {?& => '&amp;', ?> => '&gt;'}
  10. subs.default = '?'
  11. 'You & I > he & they!'.gsub(/[[:punct:]]/, subs)
  12. #=> "You &amp; I &gt; he &amp; they?"