Sha256: b76d00f883809f174ece2b447e73245799bf1c7bef3a5870706360a1169a7626
Contents?: true
Size: 1.5 KB
Versions: 10
Compression:
Stored size: 1.5 KB
Contents
# TITLE: # # String NChar # # SUMMARY: # # String extensions for accessing and manipulating # a front or end number of characters. # # AUTHORS: # # - Thomas Sawyer # class String # CREDIT Lucas Carlson and Blaine Cook # def starts_with?(prefix) self.index(prefix) == 0 end # def ends_with?(suffix) self.rindex(suffix) == size - suffix.size end # Retrns _n_ characters of the string. If _n_ is positive # the characters are from the beginning of the string. # If _n_ is negative from the end of the string. # # Alternatively a replacement string can be given, which will # replace the _n_ characters. # def nchar(n, replacement=nil) if replacement s = self.dup n > 0 ? (s[0...n] = replacement) : (s[n..-1] = replacement) return s else n > 0 ? self[0...n] : self[n..-1] end end # def lchomp!(match) if index(match) == 0 self[0...match.size] = '' self end end # def lchomp(match) if index(match) == 0 self[match.size..-1] else self.dup end end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TestStringIndex < Test::Unit::TestCase def test_nchar assert_equal( "abc", "abcxyz".nchar(3) ) assert_equal( "xyz", "abcxyz".nchar(-3) ) assert_equal( "HIxyz", "abcxyz".nchar(3, 'HI') ) assert_equal( "abcHI", "abcxyz".nchar(-3, 'HI') ) end end =end
Version data entries
10 entries across 10 versions & 1 rubygems